【问题标题】:'invalid_grant' error on API call with RestSharp使用 RestSharp 调用 API 时出现“invalid_grant”错误
【发布时间】:2021-01-19 11:52:08
【问题描述】:

我需要将该 curl 请求转换为 c#。我正在使用 RestSharp。卷曲请求:

 > curl -X POST -i https://gw.api.alphabank.eu/sandbox/auth/token \

    -u "{{client_id}}:{{client_secret}}" \

    -d "grant_type=client_credentials&scope=account-info-setup"

我尝试了以下代码,但最终得到了“invalid_grant”错误作为响应。 任何想法我做错了什么?

我的代码:

        var client = new RestClient(url);
        var request = new RestRequest();

        request.Method = Method.POST;
        client.Authenticator = new HttpBasicAuthenticator(ABclientID, ABclientSecret);
        request.AddParameter("grant_type", "client_credentials");
        request.AddParameter("scope", "account-info-setup");

        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/x-www-form-urlencoded"; };

        IRestResponse response = client.Execute(request);

【问题讨论】:

    标签: c# post restsharp


    【解决方案1】:

    这个示例代码是由 Postman 生成的,它适用于我的 api,它接受 application/x-www-form-urlencoded,你能像这样添加你的参数吗?或者创建并让您的请求在 Postman 上运行并生成到 C#-RestSharp 它通常对我有用,只需稍作更改。

    var client = new RestClient("url");
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("userId", "1234");
    request.AddParameter("count", "5");
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
    

    【讨论】:

      【解决方案2】:

      原来“invalid_grant”意味着错误的凭据。我给了错误的客户秘密。 更正client_secret后请求成功。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-29
        • 2021-04-11
        • 2018-07-10
        • 1970-01-01
        • 2017-03-06
        • 1970-01-01
        • 1970-01-01
        • 2017-10-17
        相关资源
        最近更新 更多