我是SalesForce的新手,正在努力掌握基础知识,因为我们将在下周开始一个大型集成项目.对于至少部分集成,我们需要访问SalesForce Rest API.为此,我团队中的另一位绅士和我使用Postman做了一些基本的调用原型,取得了很好的成功,特别是围绕OAuth2身份验证.我们的问题是,虽然Postman中的一切似乎都运行良好,但只要我们切换到使用C#进行调用,我们就会开始收到错误,而不是我们的身份验证令牌.响应是HTTP状态400错误请求,响应的内容是
{"error":"invalid_grant","error_description":"authentication failure"}.
Run Code Online (Sandbox Code Playgroud)
以下是我们尝试过的一些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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:这是我们尝试过的许多实现之一,包括使用HttpWebRequest和WebClient,都具有相同的结果.
除了我们自己的代码,我们还尝试从GitHub存储库developerforce/Force.com-Toolkit-for-NET中提取代码,该代码也提出了相同的问题.
到目前为止,我们有两天的时间来解决这个问题而没有运气,我们已经让SalesForce顾问难以理解为什么它不起作用.我们今天下午尝试重置安全令牌也无济于事.我在线查看了很多文章(包括StackOverflow),其中大多数文章指出了以下问题:
似乎Postman正在做的事情我们不在我们的要求中,但我似乎无法确定它是什么.我们甚至捕获了Postman和我们的一个客户的请求,并将它们分开并且它们以相同的方式返回.
任何帮助将不胜感激.
小智 18
如果你还没有解决这个问题,我想这可能是你的问题.所以根据这个: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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3558 次 |
| 最近记录: |