Php 即使我使用刷新令牌,google drive api访问令牌也会在12小时内过期

Php 即使我使用刷新令牌,google drive api访问令牌也会在12小时内过期,php,google-sheets,google-api,google-drive-api,google-api-php-client,Php,Google Sheets,Google Api,Google Drive Api,Google Api Php Client,我正在使用php库google-api-php-client-2.2.0 我试图通过crontab执行php脚本,以每小时的值自动更新googledrive电子表格 下面是我如何让我的客户使用谷歌硬盘服务 function getClient() { $client = new Google_Client(); $client->setApplicationName(APPLICATION_NAME); $client->setScopes(SCOPES);

我正在使用php库google-api-php-client-2.2.0

我试图通过crontab执行php脚本,以每小时的值自动更新googledrive电子表格

下面是我如何让我的客户使用谷歌硬盘服务

function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfig(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');
    $client->setIncludeGrantedScopes(true);

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
        $client->setAccessToken($accessToken);
        if ($client->isAccessTokenExpired()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
            $accessToken = json_decode(file_get_contents($credentialsPath), true);
            $client->setAccessToken($accessToken);
        }
    }

    return $client;
}
下面是一段代码,它可以搜索正确的google驱动器文件,并在找到后进行更新

define('APPLICATION_NAME', 'Drive API PHP Quickstart');
define('CREDENTIALS_PATH', __DIR__ . '/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(Google_Service_Drive::DRIVE)));

$client = getClient();
$service = new Google_Service_Drive($client);

$optParams = array(
    'pageSize' => 10,
    'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);

$data = get_results_as_string($all); // this is data to be updated with

if (count($results->getFiles()) == 0) {
    print "No files found.\n";
}else{
    foreach ($results->getFiles() as $file) {
        if ($file->getName() == $GOOGLE_SPREADSHEET_NAME){
            $fileId = $file->getId();
            $emptyFile = new Google_Service_Drive_DriveFile();
            $service->files->update($fileId, $emptyFile, array(
                'data' => $data,
                'mimeType' => 'text/csv',
                'uploadType' => 'media',
                'fields' => 'id')
            );
        }
    }
}
我希望当我从客户机\u SECRET\u路径文件中获取的访问令牌过期时,我将从同一文件中的刷新令牌中获取新的访问令牌(因为我已经对此进行了适当的检查)。 我用已获取的内容覆盖文件,并进一步更新和例行程序

然而,它大约工作12个小时(我每小时运行一次),然后停止工作。
如果您能在这方面提供帮助,我将不胜感激。

我发现了这一点,下面几行是用来查找文件的:

$optParams = array(
    'pageSize' => 10,
    'fields' => 'nextPageToken, files(id, name)'
);
默认的pageSize值是10,太小了。一段时间后,您要查找的文档ID将不会在前10个结果中返回。变量在[1;1000]范围内可配置
我输入了1000,它为我解决了这个问题。

我发现了这个问题,下面几行是用来查找文件的:

$optParams = array(
    'pageSize' => 10,
    'fields' => 'nextPageToken, files(id, name)'
);
默认的pageSize值是10,太小了。一段时间后,您要查找的文档ID将不会在前10个结果中返回。变量在[1;1000]范围内可配置
我输入了1000,它为我解决了问题。

Define停止工作您的错误消息是什么。您选择不使用服务帐户的任何原因?仅仅使用sheets api不是更容易吗?定义停止工作您的错误消息到底是什么。您选择不使用服务帐户的任何原因?仅仅使用sheets api不是更容易吗?