使用 C# HttpClient 登录 Salesforce

2024-03-22

我似乎无法让这段代码工作:

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 在我开始工作之前的响应。

我试图模仿的 CURL 代码是:

curl [salesforce url] -d "grant_type=password" -d "client_id=[clientId]" -d "client_secret=[clientSecret]" -d "username=[username]" -d "password=[password]"

^ 运行此命令后,服务器响应:

{"access_token":"[token]","instance_url":[etc...]}"

我希望在 C# 中得到相同的响应 - 到目前为止,这非常烦人。


client.PostAsync("https://login.salesforce.com/services/oath2/token")有错字 - 应该是oauth2,你漏掉了一封信。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 C# HttpClient 登录 Salesforce 的相关文章

随机推荐