2016-07-28 77 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