Harish Mahajan
Harish Mahajan

Reputation: 3294

salesforce api error ReasonPhrase: 'Bad Request'

I access this https://test.salesforce.com/services/oauth2/authorize werbservice and get a code in the response. Now using this code i want to access https://login.salesforce.com/services/oauth2/token

To access both methods i am using c#.net.

When i am trying to access this https://login.salesforce.com/services/oauth2/token then i am getting following error.

{
 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\"}"

I tried whole day but i could not solve the issue. Below is my code. You can check it and please suggest me what is wrong there. My code is here.

            HttpClient authClient = new HttpClient();

            string sfdcConsumerKey = "3MVG9e2mBbsdfsdfsdfZnmgdbcsdfM6nG7.s.sffsdfsfbgfbvdfgdfgyeinHsdfsf";
            string sfdcConsumerSecret = "54324324324324";
            string sfdcUserName = "[email protected]";
            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();

Upvotes: 0

Views: 1089

Answers (1)

nologo
nologo

Reputation: 6278

hey check out this git REPO for oautho provider for salesforce:

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

pretty useful wrapper for it.

Upvotes: 1

Related Questions