代码之家  ›  专栏  ›  技术社区  ›  cespon Tom

Google API致命错误:未捕获逻辑异常:刷新令牌必须传入或设置为setAccessToken的一部分

  •  0
  • cespon Tom  · 技术社区  · 8 年前

    PHP Quickstart

    致命错误:未捕获逻辑异常:必须传入刷新令牌 或在中设置为setAccessToken的一部分

    堆栈跟踪:

    #0/app/gmail。菲律宾比索(32): Google_客户端->fetchAccessTokenWithRefreshToken(NULL)

    #2{main}被抛出

    我修改了 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 ,但没有结果。为什么会出现这种错误?

    1 回复  |  直到 8 年前
        1
  •  0
  •   cespon Tom    8 年前

    Alex Blex 我能够注意到,我第一次收到令牌时 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\u令牌和file\u put\u内容删除refresh\u令牌 当这种情况再次发生时。

    按以下方式修改代码将合并到原始访问中 新的令牌。这样你就可以