0
我使用PHP库,谷歌的API-PHP-客户端2.2.0谷歌驱动的API访问令牌〜12小时期满甚至当我使用刷新令牌
我试图自动化一个谷歌驱动的更新电子表格值可以通过crontab每小时通过执行PHP脚本
这是我如何得到我的客户端与谷歌驱动的服务工作
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;
}
以下是搜索正确的谷歌驱动器文件和更新代码它曾经发现
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')
);
}
}
}
我期望当访问令牌从CLIENT_SECRET_PATH文件中抓取到期时 - 我将获取(因为我有适当的检查)来自刷新令牌的新访问令牌我在同一个文件中。 我用所提取的内容覆盖文件,并进一步更新和例程。
然而,这工作约12小时左右(我每小时运行一次),然后停下来工作。 感激,如果你能帮助这个请
定义停止工作你的错误信息到底是什么。您选择不使用服务帐户的任何原因?使用表单API会不会更容易? – DaImTo