0

任何帮助在这里表示赞赏。我只是尝试对 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();
4

2 回答 2

1

只是添加一个链接回官方文档,https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm

“您必须将用户的安全令牌附加到他们的密码 安全令牌是 Salesforce 自动生成的密钥。例如,如果用户的密码是 mypassword,他们的安全令牌是 XXXXXXXXXX,那么为此参数提供的值必须是 mypasswordXXXXXXXXXX 。”

于 2019-06-15T13:33:28.490 回答
0

如果 Salesforce 远程访问区域中未列出您的 IP 地址,则密码应为密码+安全令牌

于 2014-03-29T07:15:31.263 回答