使用C#HttpClient登录到Salesforce [英] Logging into Salesforce with C# HttpClient
问题描述
我似乎无法使该代码正常工作:
I can't seem to get this code to work:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://login.salesforce.com");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
client.DefaultRequestHeaders.Add("User-Agent", "SFAPIClient"); //Random Client
var parameters = new Dictionary<string, string>();
parameters["grant_type"] = "password";
parameters["client_id"] = CLIENT_ID;
parameters["client_secret"] = CLIENT_SECRET;
parameters["username"] = t_username.Text;
parameters["password"] = t_password.Text;
var response = await client.PostAsync("https://login.salesforce.com/services/oath2/token", new FormUrlEncodedContent(parameters));
var responseString = await response.Content.ReadAsStringAsync();
Salesforce始终以此URL不再存在进行响应,这是CURL在我得到响应之前所进行的响应
Salesforce keeps responding with "this url no longer exists" which is what CURL was responding with before I got it working.
我要模仿的CURL代码是:
The CURL code I'm trying to mimic is:
curl [salesforce url] -d grant_type =密码 -d client_id = [clientId] -d client_secret = [clientSecret] -d用户名= [用户名] -d密码= [密码]
^运行服务器后,响应如下:
^ after running this the server responds with:
{ access_token: [token], instance_url:[etc ...]}
我希望在C#中得到相同的响应-到目前为止,这一直很烦人。
I'm hoping to get the same response in C# - it's been pretty annoying so far.
卷曲输出:
推荐答案
client.PostAsync( https://login.salesforce。 com / services / oath2 / token)
有一个错字-应该为 oauth2
,您缺少字母。
client.PostAsync("https://login.salesforce.com/services/oath2/token")
has a typo - it should be oauth2
, you're missing a letter.
您还可以查看 Salesforce的.NET SDK 将该特定序列包装到一个衬里中。
You could also have a look at Salesforce's .NET SDK which wraps this particular sequence into a one liner.
这篇关于使用C#HttpClient登录到Salesforce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!