小编Cha*_*key的帖子

来自C#客户端的SalesForce身份验证出错

我是SalesForce的新手,正在努力掌握基础知识,因为我们将在下周开始一个大型集成项目.对于至少部分集成,我们需要访问SalesForce Rest API.为此,我团队中的另一位绅士和我使用Postman做了一些基本的调用原型,取得了很好的成功,特别是围绕OAuth2身份验证.我们的问题是,虽然Postman中的一切似乎都运行良好,但只要我们切换到使用C#进行调用,我们就会开始收到错误,而不是我们的身份验证令牌.响应是HTTP状态400错误请求,响应的内容是

{"error":"invalid_grant","error_description":"authentication failure"}.
Run Code Online (Sandbox Code Playgroud)

以下是我们尝试过的一些C#代码的示例:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;

namespace SFTokenTest
{
    internal class Program
    {
        private const string LoginUrl = "https://test.salesforce.com/services/oauth2/token";

        private static void Main(string[] args)
        {
            FormUrlEncodedContent content = new FormUrlEncodedContent(new []
            {
            new KeyValuePair<string, string>("grant_type",HttpUtility.UrlEncode("password")),
            new KeyValuePair<string, string>("password",HttpUtility.UrlEncode("PASSWORD")),
            new KeyValuePair<string, string>("username", HttpUtility.UrlEncode("USERNAME")),
            new KeyValuePair<string, string>("client_id",HttpUtility.UrlEncode("CLIENTID")),
            new KeyValuePair<string, string>("client_secret",HttpUtility.UrlEncode("CLIENTSECRET"))
            });
            HttpResponseMessage response;
            using (HttpClient client = new HttpClient())
            {
                response = client.PostAsync(LoginUrl, content).Result;
            }

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine(response.StatusCode);

            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这是我们尝试过的许多实现之一,包括使用HttpWebRequest和WebClient,都具有相同的结果.

除了我们自己的代码,我们还尝试从GitHub存储库developerforce/Force.com-Toolkit-for-NET中提取代码,该代码也提出了相同的问题. …

c# salesforce

6
推荐指数
1
解决办法
3558
查看次数

标签 统计

c# ×1

salesforce ×1