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