我正在尝试向C#中的salesforce api发送POST请求,但我收到错误请求错误400.
错误:类型' System.Net.WebException'的未处理异常 发生在System.dll
中
其他信息:远程服务器返回错误:(400)错误请求。
var request = (HttpWebRequest)WebRequest.Create("https://login.salesforce.com/services/oauth2/token");
var postData = "grant_type=password";
postData += "&client_id=A73MVG9qBb0Fl2vjGhrbZAN2hk9zfTsODFzWH";
postData += "&client_secret=35027716280633505312";
postData += "&username=amir@gmail.com";
postData += "&password=1234567890Fu0qqKrUodRcpMLuHrviyqX3u";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
我向Postman发送了POST请求,它有效。但使用Postman for C#生成的代码会返回相同的错误。
var client = new RestClient("https://login.salesforce.com/services/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("postman-token", "082809c3-f3f4-14f1-81f7-d6832618f905");
request.AddHeader("cache-control", "no-cache");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=A73MVG9qBb0Fl2vjGhrbZAN2hk9zfTsODFzWH&client_secret=35027716280633505312&username=amir%40gmail.com&password=1234567890Fu0qqKrUodRcpMLuHrviyqX3u", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);