salesforce api错误ReasonPhrase:'错误请求'

时间:2016-08-30 17:17:59

标签: c# asp.net salesforce

我访问此https://test.salesforce.com/services/oauth2/authorize werbservice并在响应中获取代码。 现在使用此代码我想访问https://login.salesforce.com/services/oauth2/token

要使用c#.net访问这两种方法。

当我尝试访问此https://login.salesforce.com/services/oauth2/token时,我会收到以下错误。

{
 StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:  System.Net.Http.StreamContent, Headers:
{
  Strict-Transport-Security: max-age=10886400; includeSubDomains; preload
  Content-Security-Policy-Report-Only: default-src https:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /_/ContentDomainCSPNoAuth?type=login
  Pragma: no-cache
  Transfer-Encoding: chunked
  Cache-Control: no-store, no-cache
  Date: Tue, 30 Aug 2016 16:08:22 GMT
  Set-Cookie: BrowserId=pANkulkPTdesvcocxGm5Q;Path=/; Domain=.salesforce.com;Expires=Sat, 29-Oct-2016 16:08:22 GMT
  Content-Type: application/json; charset=UTF-8
  Expires: Thu, 01 Jan 1970 00:00:00 GMT
 }
}


 "{\"error\":\"invalid_grant\",\"error_description\":\"authentication failure\"}"

我试了一整天但是我无法解决这个问题。 以下是我的代码。你可以检查一下,请告诉我那里有什么问题。 我的代码在这里。

            HttpClient authClient = new HttpClient();

            string sfdcConsumerKey = "3MVG9e2mBbsdfsdfsdfZnmgdbcsdfM6nG7.s.sffsdfsfbgfbvdfgdfgyeinHsdfsf";
            string sfdcConsumerSecret = "54324324324324";
            string sfdcUserName = "harish@gmail.com";
            string sfdcPassword = "mypassword123456";
            string sfdcToken = "fsdcbvcvbfgerwcxvx";
            string loginPassword = sfdcPassword + sfdcToken;
            string code = Request.QueryString["code"];
            HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
            {
                {"grant_type","authorization_code"},
                {"code",code},
                {"client_id",sfdcConsumerKey},
                {"client_secret",sfdcConsumerSecret},
                {"username",sfdcUserName},
                {"password",loginPassword},
                {"redirect_uri","http://localhost:1474/Default.aspx"}                  
            }
            );


            HttpResponseMessage message = await authClient.PostAsync("https://login.salesforce.com/services/oauth2/token", content);

            string responseString = await message.Content.ReadAsStringAsync();

1 个答案:

答案 0 :(得分:1)

嘿,看看这个git REPO for oautho provider for salesforce:

https://github.com/TerribleDev/OwinOAuthProviders/blob/master/src/Owin.Security.Providers.Salesforce/SalesforceAuthenticationHandler.cs

非常有用的包装。