[英]Salesforce Rest API OAuth - Bad Response Error
Any help here is appreciated. 感谢您的帮助。 Im simply trying to do the basic call to the Oauth Username-Password Flow. 我只是试图对Oauth用户名-密码流进行基本调用。 Its giving me a Bad Request (400) Error right when I try to call GetResponse(). 当我尝试调用GetResponse()时,它给了我一个Bad Request(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();
Just to add a link back to offical doc, https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm 只是要添加回官方文档的链接, https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm
"You must append the user's security token to their password A security token is an automatically-generated key from Salesforce. For example, if a user's password is mypassword, and their security token is XXXXXXXXXX, then the value provided for this parmeter must be mypasswordXXXXXXXXXX." “您必须在用户的密码后附加用户的安全令牌。安全令牌是Salesforce自动生成的密钥。例如,如果用户的密码是mypassword,而他们的安全令牌是XXXXXXXXXX,则为此参数提供的值必须是mypasswordXXXXXXXXXX ”。
如果不是您的IP地址白色(在Salesforce远程访问区域中列出),则密码应为密码+安全令牌
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.