【问题标题】:Google API Fatal error: Uncaught LogicException: refresh token must be passed in or set as part of setAccessTokenGoogle API 致命错误:未捕获的 LogicException:刷新令牌必须传入或设置为 setAccessToken 的一部分
【发布时间】:2018-02-24 22:26:14
【问题描述】:

使用PHP Quickstart代码,发现一个问题:需要刷新token时,代码返回如下错误:

致命错误:未捕获的LogicException:必须传入刷新令牌 或设置为 setAccessToken 的一部分 /app/vendor/google/apiclient/src/Google/Client.php:258

堆栈跟踪:

#0 /app/gmail.php(32): Google_Client->fetchAccessTokenWithRefreshToken(NULL)

#1 /app/test.php(14): getClient()

#2 {main} 投入 /app/vendor/google/apiclient/src/Google/Client.php 第 258 行

我像这样修改了getClient() 函数:

function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfig(CLIENT_SECRET_PATH);
    $client->setRedirectUri(REDIRECT_URL);
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);

    if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } 
    else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        return printf("<a href='%s' target='_blank'>auth</a><br />", $authUrl);
    }
    $client->setAccessToken($accessToken);

    // Refresh the token if it's expired.
    // ERROR HERE !!
    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }
    return $client;
}

第一次认证后(使用getClient()函数中创建的链接),当用户登陆REDIRECT_URL时,会执行callbackAuth()函数:

function callbackAuth() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfig(CLIENT_SECRET_PATH);
    $client->setRedirectUri(REDIRECT_URL);
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);

    // Request authorization from the user.
    $authCode = trim($_GET['code']);

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Store the credentials to disk.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s\n", $credentialsPath);

    $client->setAccessToken($accessToken);

    return $client;
}

我尝试应用其他相关stackoverflow question 的解决方案,但没有结果。为什么会出现这个错误?

【问题讨论】:

  • 您是否确认“从文件加载先前授权的凭据”的令牌。有刷新令牌,密钥是refresh_token?该错误表明它没有。
  • 我检查了令牌,它只包含access_tokentoken_typeexpires_increated 属性。如何检索 refresh_token 属性?
  • 错误没有说。请参阅文档。您如何获得 access_token?
  • 我使用 callbackAuth() 函数获取 access_token。问题已更新以获取更多详细信息。

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


【解决方案1】:

感谢Alex Blex,我注意到我第一次收到令牌时有refresh_token,但在第一次请求之后,refresh_token 没有被存储。解决方法如下(from this answer):

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    $newAccessToken = $client->getAccessToken();
    $accessToken = array_merge($accessToken, $newAccessToken);
    file_put_contents($credentialsPath, json_encode($accessToken));
}

refresh_token 仅在第一次请求时返回。当你 再次刷新访问令牌,它会返回所有内容,除了 refresh_token 和 file_put_contents 删除了 refresh_token 当这种情况发生第二次时。

修改如下代码会合并到原来的访问 与新的令牌。这样你就可以 为将来的请求保留您的 refresh_token。

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 2017-01-11
    • 2019-02-23
    • 1970-01-01
    • 2022-06-18
    • 2018-10-21
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多