任何帮助在这里表示赞赏。我只是尝试对 Oauth 用户名-密码流进行基本调用。当我尝试调用 GetResponse() 时,它给了我一个错误的请求 (400) 错误。
string sessionId = String.Empty;
string url = ConfigurationManager.AppSettings["SfdcLoginUrl"].ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://test.salesforce.com/services/oauth2/token");
request.Method = "POST";
request.ContentType = "application/json";
StringBuilder sb = new StringBuilder();
sb.Append("grant_type=password&");
sb.Append("client_id=" + ConfigurationManager.AppSettings["ClientId"].ToString() + "&");
sb.Append("client_secret=" + ConfigurationManager.AppSettings["ClientSecret"].ToString() + "&");
sb.Append("username=" + HttpUtility.UrlPathEncode(username) + "&");
sb.Append("password=" + HttpUtility.UrlPathEncode(password));
System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(sb.ToString());
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
sessionId = reader.ReadToEnd();