【问题标题】:(400) Bad Request : Get AccessToken of Salesforce(400) 错误请求:获取 Salesforce 的 AccessToken
【发布时间】:2017-07-24 22:40:58
【问题描述】:

我正在尝试通过我的应用程序中的 oauthSalesforce 获取所有联系人。我在我的回调页面上尝试通过 API Token Url 获取 访问令牌 时收到 远程服务器返回错误:(400) Bad Request.。即使当我复制 URL 并直接粘贴到 URL 时,它也会给我正确的数据。但无法获取访问令牌。

我正在使用以下方法获取 访问令牌。请专家建议我在哪里做错了。同样的方法适用于 Google 和 LinkedIn。

public async Task<TokenResponse> GetAccessToken(string code,string tokenUrl,string consumerKey,string consumerSecretKey,string redirecturl)
    {

        string responseFromServer = "";
        var webRequest = WebRequest.Create(tokenUrl);

        const string queryStringFormat = @"code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code";
        string contents = string.Format(queryStringFormat
                                           , code
                                           , consumerKey
                                           , consumerSecretKey
                                           , redirecturl);

        webRequest.Method = "POST";
        byte[] postcontentsArray = Encoding.UTF8.GetBytes(contents);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = postcontentsArray.Length;
        using (Stream requestStream = webRequest.GetRequestStream())
        {
            requestStream.Write(postcontentsArray, 0, postcontentsArray.Length);
            requestStream.Close();
            WebResponse response = webRequest.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        responseFromServer = reader.ReadToEnd();
                        reader.Close();
                        responseStream.Close();
                        response.Close();
                    }
                }
            }

        }
        return JsonConvert.DeserializeObject<TokenResponse>(responseFromServer);
    }

请帮我解决这个问题。

【问题讨论】:

  • HTTP 响应的正文是什么?这通常可以解释错误。
  • 你能澄清一下你的意思吗:“即使我复制了 URL 并直接粘贴到 URL,它也给了我正确的数据”?
  • 在调试时我得到了获取响应的 URL。我复制了那个 URL 并直接粘贴到浏览器上,它显示了带有访问令牌的正确 XML。但是在代码中给出错误。
  • 使用浏览器,您正在执行GET,但您的代码正在执行POST。您确定该服务需要表单 url 编码的帖子正文吗?
  • 是的,当我通过 GET 方法尝试它时,它会给出错误并说需要 POST 方法。

标签: c# asp.net oauth-2.0 salesforce


【解决方案1】:

我正在毫无问题地连接到 Salesforce,但我使用的是 HttpClient 而不是 WebRequest。

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
            {
                {"grant_type", "password"},
                {"client_id", _clientId},
                {"client_secret", _clientSecret},
                {"username", _userName},
                {"password", _password}
            }
                );

                using (var httpClient = new HttpClient())
                {
                    var message =
                        await httpClient.PostAsync(_authorizationUrl, content).ConfigureAwait(false);
                    var responseString = await message.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var obj = JObject.Parse(responseString);

                    var oauthToken = (string)obj["access_token"];
                    var serviceUrl = (string)obj["instance_url"];
}

【讨论】:

  • 我已根据我的要求对此进行了一些更改。谢谢
  • 在本地运行良好,但在部署到 azure 功能后出现 400 错误
  • 我遇到了同样的错误问题 - {"error":"invalid_grant","error_description":"authentication failure"}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 2018-07-08
  • 2017-11-27
  • 2013-06-30
  • 1970-01-01
相关资源
最近更新 更多