【问题标题】:"{\"error\":\"unsupported_grant_type\",\"error_description\":\"Grant Type is NULL\"}""{\"error\":\"unsupported_grant_type\",\"error_description\":\"授权类型为NULL\"}"
【发布时间】:2019-05-21 04:07:34
【问题描述】:

我收到以下回复:

"{\"error\":\"unsupported_grant_type\",\"error_description\":\"Grant 类型为 NULL\"}"

我尝试了几种不同的方法来构建它想要的 JSON 字符串,但我没有任何运气。我看过一些样本,人们让它工作,但他们必须改变它。

这是我的代码:

public string PostPayment([FromBody]Payment_DTO payment)
{
    //Request token
    var client = new RestClient(_EndPoint);
    var request = new RestRequest(Method.POST);
    string json = BuildTokenRequest();
    string svcCredentials = 
    Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(_UserName + ":" + 
    _Password));

    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Authorization", "Basic " + svcCredentials);
    request.AddHeader("content-type", "application/x-www-form- 
    urlencoded");
    request.AddParameter("application/json", json, 
    ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);

    return response.Content.ToString();
}

我认为问题在于我的 json builder 函数本身。我确定我在这里做错了什么:

public string BuildTokenRequest()
{
    //string request = "grant_type=" + _Password + "&client_id=" + _UserName + "&client_secret=" + _Password + "$username=" + _UserName + "&password=" + _Password;
    string request = "client_id="+ _UserName + "secret=" + _Password + "grant_type=client_credentials";

    return JsonConvert.SerializeObject(request);
}

【问题讨论】:

    标签: c# .net json serialization json.net


    【解决方案1】:

    您的代码不会产生任何类似于有价值的 JSON 对象的东西,它只是将一个简单的字符串序列化为 JSON。当你这样做时,你真正从另一端得到的只是……一个简单的字符串——因为那已经是有效的 JSON。序列化程序无法知道您打算将这些作为单独的字段,它只会看到一段长文本。它无法赋予它任何额外的意义。

    例如:

    string _UserName = "123";
    string _Password = "abc";
    string request = "client_id=" + _UserName + "secret=" + _Password + "grant_type=client_credentials";
    Console.WriteLine(JsonConvert.SerializeObject(request));
    

    只会输出

    "client_id=123secret=abcgrant_type=client_credentials"
    

    演示:https://dotnetfiddle.net/DTDDjI

    现在,正如我所说,从技术上讲,它是有效的 JSON,但它不太可能是远程服务器所期望的 - 再次,它不会知道它需要解析该字符串并从中提取值。我无法指定您的远程 API 的规范(因为您没有告诉我们您正在调用哪个端点或将我们链接到任何文档),但我想会期待一个具有单独字段中的值的对象。要从 C# 中获取这种东西,您需要从 C# 对象开始:

    例如:

    string _UserName = "123";
    string _Password = "abc";
    var request = new { client_id = _UserName, secret = _Password, grant_type = "client_credentials" };
    Console.WriteLine(JsonConvert.SerializeObject(request));
    

    会输出

    {"client_id":"123","secret":"abc","grant_type":"client_credentials"}
    

    演示:https://dotnetfiddle.net/wCpMhV

    注意使用包含离散字段的匿名对象传递给 JSON 序列化器,然后将包含离散字段的对象作为结果。

    正如我所说,我无法检查这是否正是远程服务器所期望的布局,但您应该能够检查文档以查看其是否正确。如果没有,您现在希望了解如何正确生成符合规范的有用 JSON 对象。


    还有一点。这行代码:

    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    

    是不必要的。您可以删除它,因为

    a) 它为包含 JSON 和

    的请求设置了错误的内容类型

    b) 它下面的行将(根据RestSharp docs 在一次调用中设置正确的内容类型标头和 JSON 正文内容。

    【讨论】:

    • 嗯,它适用于 Paypal。所以你的建议将它构建为一个字符串数组?我会试一试,谢谢。
    • @mholmes 为了清楚起见,{"client_id":"123","secret":"abc","grant_type":"client_credentials"} 不是一个数组,而是一个对象。如果您不知道 JSON 对象的区别或确实不了解其一般结构,那么现在可能是更详细地研究它的明智时机。
    • 我对 Json 非常了解,但我不懂 PayPal 文档,哈哈,这就是我在这里试图弄清楚的原因。尽管它是我的 json 字符串的结构,但您可能是正确的。
    【解决方案2】:

    就我而言,问题是我的请求被错误地编码为"content-type" : "application/x-www-form-urlencoded"。 基于this solution,我以不同的方式构建了我的request.httpBody,并且成功了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2019-04-21
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      相关资源
      最近更新 更多