【问题标题】:Google Docs Api v2 Invalid grant error with Malformed Auth Code descriptionGoogle Docs Api v2 Invalid grant error with Malformed Auth Code description
【发布时间】:2018-06-08 16:32:28
【问题描述】:

我刚刚安装了 Google API 2.0,设置了我的应用程序并尝试授权用户,但我不断收到此错误:

array(2) {
  ["error"]=>
  string(13) "invalid_grant"
  ["error_description"]=>
  string(20) "Malformed auth code."
}

为了创建授权链接,我在\Google_Client 中使用了函数$oGoogleClient->createAuthUrl();

它把我带到授权页面,然后返回到我的授权页面,网址中的代码如下:

http://example.com/authorize/?code=4/AABBv8nQ5N4mqrOTANDphl_L4ROPnzK6yckffDu-dnlIJGE9ZOcXo9eehUVbzbExbMuhCZQAb5zu9_BIS-VI4E4#

为了处理这个请求,我使用了\Google_Client中的api函数$oGoogleClient->fetchAccessTokenWithAuthCode($sCode);

起初我以为是因为代码末尾的#,因为PHP只获取到那个标签之前的代码参数,所以我硬编码它来测试,但结果是相同的错误消息验证码格式错误。

你知道如何解决这个问题吗?

更新:我已将代码移至另一台服务器,它将正确授权代码并检索访问令牌。我猜应该是服务器内部的东西,但我不知道是什么!

【问题讨论】:

    标签: google-api google-api-php-client google-api-client


    【解决方案1】:

    我正在使用Node.js googleapis 客户端库,这是我的情况:

    url hash 片段中的授权码是由encodeURIComponentapi 编码的,所以如果你通过这个码来请求访问令牌。会报错:

    { "error": "invalid_grant", "error_description": "Malformed auth code." }

    所以我使用decodeURIComponent来解码授权码。

    decodeURIComponent('4%2F_QCXwy-PG5Ub_JTiL7ULaCVb6K-Jsv45c7TPqPsG2-sCPYMTseEtqHWcU_ynqWQJB3Vuw5Ad1etoWqNPBaGvGHY')
    

    解码后授权码为:

    "4/_QCXwy-PG5Ub_JTiL7ULaCVb6K-Jsv45c7TPqPsG2-sCPYMTseEtqHWcU_ynqWQJB3Vuw5Ad1etoWqNPBaGvGHY"
    

    【讨论】:

      猜你喜欢
      • 2022-11-08
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      相关资源
      最近更新 更多