2016-07-28 74 views
6

我是Salesforce新手,正在努力加快基礎,因爲我們將在下週開始大型集成項目。對於我們至少部分的集成,我們需要訪問SalesForce Rest API。爲此,我和我的團隊中的另一位紳士利用Postman取得了很好的成功,特別是在OAuth2身份驗證方面做了一些基本的呼叫原型設計。我們的問題是,儘管郵差中的一切似乎都能正常工作,但只要我們切換到使用C#進行調用,就會發生錯誤,而不是我們的身份驗證令牌。響應是HTTP狀態400錯誤的請求和響應的內容是來自C#客戶端的SalesForce身份驗證錯誤

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

下面是一些C#代碼的示例中,我們曾嘗試:

using System; 
using System.Collections.Generic; 
using System.Net.Http; 
using System.Web; 

namespace SFTokenTest 
{ 
    internal class Program 
    { 
     private const string LoginUrl = "https://test.salesforce.com/services/oauth2/token"; 

     private static void Main(string[] args) 
     { 
      FormUrlEncodedContent content = new FormUrlEncodedContent(new [] 
      { 
      new KeyValuePair<string, string>("grant_type",HttpUtility.UrlEncode("password")), 
      new KeyValuePair<string, string>("password",HttpUtility.UrlEncode("PASSWORD")), 
      new KeyValuePair<string, string>("username", HttpUtility.UrlEncode("USERNAME")), 
      new KeyValuePair<string, string>("client_id",HttpUtility.UrlEncode("CLIENTID")), 
      new KeyValuePair<string, string>("client_secret",HttpUtility.UrlEncode("CLIENTSECRET")) 
      }); 
      HttpResponseMessage response; 
      using (HttpClient client = new HttpClient()) 
      { 
       response = client.PostAsync(LoginUrl, content).Result; 
      } 

      Console.WriteLine(response.Content.ReadAsStringAsync().Result); 
      Console.WriteLine(response.StatusCode); 

      Console.ReadLine(); 
     } 
    } 
} 

注:這是我們嘗試過的許多實現中的一個包括使用HttpWebRequest和WebClient,所有這些實現都具有相同的結果。

除了我們自己的代碼之外,我們還嘗試從GitHub存儲庫developerforce/Force.com-Toolkit-for-NET中提取代碼,它也提出了相同的問題。

到目前爲止,我們需要兩天的時間才能解決這個問題,但沒有運氣,我們已經讓我們的SalesForce顧問難以理解它爲什麼不起作用。我們今天下午嘗試重置安全令牌也無濟於事。我已經通過了一些文章在網上看了(包括在計算器上),這點以下問題大部分:

  • 不正確的憑據 - 我們使用相同的憑據成功郵差,我們正試圖在我們的C#中使用應用程序,我們嘗試了重新應對並從SalesForce UI中粘貼它們,並且我們嘗試將它們鍵入我們的代碼中以防我們拾取隱形字符。 (通過憑據我包括用戶名,密碼,client_id和客戶端密碼。)
  • 錯誤或缺少內容類型標頭 - 我們肯定會發送「應用程序/ x-www-form-urlencoded」,我檢查了每個應用程序多次(並嗅探以確認它實際上正在發送)。
  • 未編碼參數 - 我已經在一些StackOverflow線程上看到了這一點,並且已經驗證我們正在編碼我們發送請求體的參數。

看來有什麼郵遞員正在做我們不在我們的要求,但我似乎無法指出它是什麼。我們甚至從郵遞員和我們的一個客戶那裏收到了要求,並將它們加以區分,然後他們又回到了原樣。

任何幫助將不勝感激。

回答

17

如果你還沒有解決這個問題,我認爲這可能是你的問題。 因此根據此:https://help.salesforce.com/apex/HTViewSolution?id=000221207 從2016年6月起,Salesforce將分階段禁用整個平臺的TLS 1.0加密。

如果您使用的是.net 4.6之前的任何版本,則表示默認情況下不會打開TLS 1.1/1.2。在這裏有一些你可以用來啓用它的選項:https://help.salesforce.com/apex/HTViewSolution?id=000221207#Inboundintegrations

對於像你這樣的控制檯應用程序,只需在我身邊進行測試,以下工作適用於我(在.net 4.5上。2):

 string LoginUrl = "https://login.salesforce.com/services/oauth2/token"; 
     //the line below enables TLS1.1 and TLS1.2 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 
     FormUrlEncodedContent content = new FormUrlEncodedContent(new[] 
     { 
     new KeyValuePair<string, string>("grant_type", "password"), 
     new KeyValuePair<string, string>("client_id","CLIENTID"), 
     new KeyValuePair<string, string>("client_secret","CLIENTSECRET"), 
     new KeyValuePair<string, string>("password","PASSWORD + SECURITYTOKEN"), 
     new KeyValuePair<string, string>("username", "USERNAME") 
     }); 
     HttpResponseMessage response; 
     using (HttpClient client = new HttpClient()) 
     { 
      response = client.PostAsync(LoginUrl, content).Result; 
     } 
     Console.WriteLine(response.Content.ReadAsStringAsync().Result); 
     Console.WriteLine(response.StatusCode); 
     Console.ReadLine(); 
+0

我們也確定這是我們的問題,我可以驗證我們做了完全相同的更改,我們的代碼現在也可以正常工作。 – ChaosMonkey

+0

非常感謝你 - 自昨天以來,我一直在努力解決同樣的問題。添加了單一的ServicePointManager行,它工作。我以爲我瘋了! – cyberspy

+0

非常感謝您的幫助。它正在爲我工​​作 – gaurav