访问令牌客户端->refreshToken 在长时间运行的脚本中停止工作

问题描述 投票:0回答:1

我有一个脚本,可以在本地文件系统上复制对特定 Google Drive 文件夹及其子文件夹所做的更改。脚本几天来一直运行良好,但今天中午,刷新代码停止运行,我找不到任何解释。

这是演示该问题的日志

2023-08-11 08:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 09:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 10:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 11:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 12:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 13:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 14:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 15:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 16:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 17:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 18:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 19:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 20:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 21:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116361) - nothing to do.
2023-08-11 21:45:02 /www/htdocs/gdrive/catchupSongBook: Downloading /SongBook/2023-08-12.lst (452 bytes)
2023-08-11 21:45:03 /www/htdocs/gdrive/catchupSongBook: Finished.  1 files found in 0 folders.  (1 files updated, 0 added.  0 new folders).  452 bytes downloaded.
2023-08-11 22:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116363) - nothing to do.
2023-08-11 23:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116363) - nothing to do.
2023-08-12 04:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116363) - nothing to do.
2023-08-12 05:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116363) - nothing to do.
2023-08-12 06:00:06 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116363) - nothing to do.
2023-08-12 07:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116363) - nothing to do.
2023-08-12 07:49:02 /www/htdocs/gdrive/catchupSongBook: Downloading /SongBook/2023-08-12.lst (452 bytes)
2023-08-12 07:49:02 /www/htdocs/gdrive/catchupSongBook: Finished.  1 files found in 0 folders.  (1 files updated, 0 added.  0 new folders).  452 bytes downloaded.
2023-08-12 08:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116366) - nothing to do.
2023-08-12 09:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116366) - nothing to do.
2023-08-12 10:00:03 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116366) - nothing to do.
2023-08-12 11:00:02 /www/htdocs/gdrive/catchupSongBook: No change in start page token (116366) - nothing to do.
[12-Aug-2023 12:00:02 America/New_York] PHP Fatal error:  Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /www/cgi-bin/GoogleRelated/DriveStuff.php:160
Stack trace:
#0 /www/cgi-bin/GoogleRelated/DriveStuff.php(82): DriveStuff->GetAuth()
#1 /www/htdocs/gdrive/catchupSongBook(52): DriveStuff->GetDriveService()
#2 {main}
  thrown in /www/cgi-bin/GoogleRelated/DriveStuff.php on line 160

这是有问题的代码。为了清楚起见,添加了有关第 160 行的注释。

    $client->setAccessType($accessType);
    $this->GetClientFromDB($clientId        // Handles inserttion of new Client information if necessary; returns as much information as we have otherwise
                        , $clientSecret
                        , $scopes_dummy     // Values below here (including this value) are passed by reference, and are updated by GetClientFromDB()
                        , $accessToken      // Updated by GetClientFromDB()
                        , $tokenType        // Updated by GetClientFromDB()
                        , $refreshToken     // Updated by GetClientFromDB()
                        , $expires          // Updated by GetClientFromDB()
                        , $created          // Updated by GetClientFromDB()
                        , $rowId            // Updated by GetClientFromDB()
                            ) ;
    $authHistoryRow = [] ;
    $authHistoryRow['authConfig'] = json_encode($client_json) ;
    $authHistoryRow['clientId'] = $clientId ;
    $authHistoryRow['clientSecret'] = $clientSecret ;
    $authHistoryRow['projectId'] = $client_json[$topKey]['project_id'] ;
    $authHistoryRow['authUri'] = $client_json[$topKey]['auth_uri'] ;
    $authHistoryRow['tokenUri'] = $client_json[$topKey]['token_uri'] ;
    $authHistoryRow['authProviderX509CertUrl'] = $client_json[$topKey]['auth_provider_x509_cert_url'] ;
    $authHistoryRow['appName'] = $appName ;
    $authHistoryRow['scopes'] = implode(',', $scopes) ;
    $authHistoryRow['accessType'] = $accessType ;
    if ((! $rowId) || $client->isAccessTokenExpired()) {
        $accessToken = null ;
        // Refresh the token...
        if ($rowId && $client->isAccessTokenExpired() && $refreshToken) {
            $authHistoryRow['refreshToken'] = $refreshToken ;
            $client->refreshToken($refreshToken) ;
            $accessTokenArray = $client->getAccessToken() ;
(line 160 follows)
            $accessToken = (array_key_exists('access_token', $accessTokenArray) ? $accessTokenArray['access_token'] : false) ;
            $expires = $accessTokenArray['expires_in'] + time() ;
            $authHistoryRow['accessToken'] = $accessTokenArray['access_token'] ;
            $authHistoryRow['expires'] = Date('Y/m/d H:i:s', $expires) ;
            }
        if ((! $accessToken) ) { // Still not able to get authorization.  Talk to user.
            printf("Authorization expired at %s\n", date('Y-m-d H:i', $expires)) ;
            $authUrl = $client->createAuthUrl();
            printf("Open this link in your browser:\n%s\n", $authUrl);
            printf("Copy verification code\nOr, if 'Page cannot displayed' (or similar), copy entire URL from browser window.\nPaste here: ");
            $authCode = trim(fgets(STDIN));
            if (preg_match('%^[:/0-9.a-zA-Z_-]+[/][?]code=([^&]+)&%', $authCode, $matches))
                $authCode = urldecode($matches[1]) ;
            // Exchange authorization code for an access token.
            $accessTokenArray = $client->fetchAccessTokenWithAuthCode($authCode);
            //$output = print_r($accessToken, true) ;
            //file_put_contents("access_token.txt", $output) ;
            }
        $accessExpiresEpoch = time() + $accessTokenArray['expires_in'] ;

当然,这段代码显然可以在这个刷新区域中得到改进(欢迎提出建议!),但无论如何,服务似乎(?)没有刷新令牌,我想了解为什么。如何找出在执行refreshToken()操作后$client->getAccessToken()返回null的原因?我可以执行哪些测试来确保刷新令牌()操作确实有效,或者如果没有,为什么不呢?

一个选择可能是获得一组新的代币 - 我可以轻松地采取这些步骤,并且有一定成功的把握。但更重要的是为什么在我这样做之前它不起作用。我们不希望每隔几天对应该运行的流程进行手动干预。

google-oauth access-token refresh-token
1个回答
0
投票

如果您的应用程序正在测试,刷新令牌将在 7 天后过期。

在同意屏幕下转到您项目的 Google 云控制台并将其设置为生产

© www.soinside.com 2019 - 2024. All rights reserved.