简体   繁体   English

Salesforce Rest API OAuth-错误的响应错误

[英]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.

相关问题 Salesforce Rest API补丁程序操作-错误的请求 - Salesforce Rest API patch operation - Bad Request salesforce api错误ReasonPhrase:“错误请求” - salesforce api error ReasonPhrase: 'Bad Request' 尝试使用REST API进行插入时,Salesforce返回未知错误 - Salesforce returns Unknown Error while trying to insert using REST API salesforce owin oauth登录返回未经授权的响应 - salesforce owin oauth login returns an unauthorized response Rackspace云文件REST API莫名其妙地收到错误的请求响应 - Rackspace cloud files REST api inexplicably getting bad request response Twitter API oAuth(错误代码214:错误的身份验证数据) - Twitter API oAuth (Error code 214: Bad authentication data) 将 DateTime 传递给 REST API 时出现“错误请求”错误 - "Bad request" error when passing DateTime to REST API 使用C#的Google语音识别REST API出现错误请求错误 - Bad Request Error with Google Speech Recognition REST API with C# 是否有可用于Salesforce REST Api的ac#wrapper? - Is there a c# wrapper available for the Salesforce REST Api? 在HttpClient中向Google Oauth2 SSO发送Rest请求时,如何解决“错误请求”错误 - How to fix “Bad request” error when sending Rest request to Google Oauth2 SSO in HttpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM