【问题标题】:RestSharp Google's OAuthRestSharp Google 的 OAuth
【发布时间】:2018-09-12 21:04:10
【问题描述】:

如果我在 Google 的 OAuth-Server 上将 RestSharp 用于 grant_type=authorization_code,我会收到以下响应:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\ngrant_type=authoriza\n^",
    "status": "INVALID_ARGUMENT"
  }
}

如果我使用 Fiddler 进行请求,一切正常,它也可以与其他 OAuth 服务器一起使用。

休息请求:

RestClient  restClient = new RestClient(@"https://www.googleapis.com/");
RestRequest restRequest = new RestRequest(@"oauth2/v4/token", Method.POST);
            restRequest.AddHeader("Accept", "application/json");
            restRequest.AddHeader("Content-Type", "application/x-www-urlencoded");
restRequest.AddParameter("grant_type", "authorization_code");
restRequest.AddParameter("client_id", "id");
restRequest.AddParameter("client_secret", "secret");
restRequest.AddParameter("code", "authcode");
restRequest.AddParameter("redirect_uri", "redirectURI");

如何解决这个问题?为什么会这样:\ngrant_type=authoriza\n^

【问题讨论】:

    标签: c# google-oauth restsharp


    【解决方案1】:

    字符串应作为单个参数添加

    POST https://accounts.google.com/o/oauth2/token
    code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code
    

    有效负载是单个 http 请求字符串,它不是 json 格式,但您返回的响应是 json 格式

    内容类型应该是

    application/x-www-form-urlencoded
    

    【讨论】:

    • RestSharp 不应该根据自动传递的参数组装那条编码的行吗?
    • 不知道我从未使用过restsharp,但看起来它在每个参数之后添加返回(\ n)应该用&将它们串在一起。我通常使用 google .net 客户端库来为您处理所有这些。
    • 谢谢,是Content-Type,只是忘记了“form”……这就是问题所在,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2023-03-07
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多