google-api-php-client - Google api php 客户端给出 "malformed auth code"错误

标签 google-api-php-client

所以我最近升级了用于 PHP 的 google api 客户端,但我一直无法让它进行身份验证并给我一个 token 。它曾经在旧库中运行良好,但是通过 composer 安装的这个新库给我带来了一些问题。这是我的代码供引用:

if (!empty($gp_auth_code) {
    $client = new Google_Client();
    $client->setAuthConfig('<path to client.json file>');

    $client->addScope(Google_Service_Oauth2::PLUS_LOGIN);

    $client->setRedirectUri('<redirect URI>');


    $token = $client->fetchAccessTokenWithAuthCode($gp_auth_code);

    $gp_access_token = $token['access_token'];
    $gp_refresh_token = $client->refreshToken($token)['refresh_token'];
}

每当我运行该代码时,它都会给我响应“无效的授权,格式错误的身份验证代码”。

有什么我做错了吗? $gp_auth_code 过去工作得很好,json 文件的路径肯定有效,而且重定向 URI 肯定也在那里。任何帮助,将不胜感激。

我基本上是在尝试访问 google plus 个人资料的信息,因此我们可以通过 google plus 注册用户。

最佳答案

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

url hash 片段中的授权码由 encodeURIComponent 编码api,所以如果你通过这个代码来请求访问 token 。它会抛出一个错误:
{ "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"

然后,一切正常。

关于google-api-php-client - Google api php 客户端给出 "malformed auth code"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50262784/

相关文章:

php - 在 Google 日历中插入事件 PHP 和 SQL

php - 谷歌 API 客户端 :"{ "错误“: "invalid_grant", "error_description": "Incorrect token type."}”

php - Google Drive API - 上传和导出文件

php - 使用 API v3 访问 Google 日历

java - Google pagespeed Insights V5 - 最小化关键请求深度

google-api - 谷歌 OAuth : can't get refresh token with authorization code

PHP YouTube API - thumbnails.set 失败

google-api - 有什么方法可以验证BigQuery API中的查询

php - 代币必须是短暂的代币,并且在合理的时间范围内

php - Google OAuth - 私有(private) IP 需要 device_id 和 device_name