2016-08-30 71 views
0

我訪問此https://test.salesforce.com/services/oauth2/authorize werbservice並獲取響應中的代碼。 現在使用這個代碼我想訪問https://login.salesforce.com/services/oauth2/tokensalesforce api錯誤ReasonPhrase:'Bad Request'

要訪問這兩種方法我使用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 = "[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(); 
+0

Salesforce StackExchange可能更有幫助。 (http://salesforce.stackexchange.com/) – KDecker

回答