Я пытаюсь обновить токен на моем PHP-скрипте, но он пока не работает.Я использую Google Drive API с PHP, мой скрипт прекрасно работает в браузере, пока не истечет токен, проверяя на других сайтах, я изменил код следующим образом:
index.php
<?php
require_once 'vendor/autoload.php';
if (!file_exists("client_id.json")) exit("Client secret file not found");
$client = new Google_Client();
$client->setAuthConfig('client_id.json');
$client->addScope(Google_Service_Drive::DRIVE);
if (file_exists("credentials.json")) {
$access_token = (file_get_contents("credentials.json"));
$client->setAccessToken($access_token);
//Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$refreshTokenSaved = $client->getRefreshToken();
$client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
$accessTokenUpdated = $client->getAccessToken();
$accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
file_put_contents("credentials.json", json_encode($accessTokenUpdated));
//I also tried in this way but it's also not working:
//$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
//file_put_contents("credentials.json", json_encode($client->getAccessToken()));
}
$drive_service = new Google_Service_Drive($client);
$files_list = $drive_service->files->listFiles(array())->getFiles(); //http://stackoverflow.com/questions/37975479/call-to-undefined-method-google-service-drive-filelistgetitems
echo json_encode($files_list);
} else {
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php
<?php
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_id.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
file_put_contents("credentials.json", json_encode($access_token));
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
Но я все еще получаю сообщение в журналах:
PHP Неустранимая ошибка: исключение Uncaught исключение 'LogicExceptionМаркер обновления 'with message' должен быть передан или установлен как часть setAccessToken 'в /var/www/drive/vendor/google/apiclient/src/Google/Client.php:266\nStack trace: \ n # 0 / var/www/drive/index.php(14): Google_Client-> fetchAccessTokenWithRefreshToken (NULL) \ n # 1 {main} \ n, брошенный в /var/www/drive/vendor/google/apiclient/src/Google/Client.phpв строке 266
Я должен удалить credentials.json и сгенерировать новый, выполнив index.php и перенаправленный на oauth2callback.php каждый час, но это не идея.
Как я могу это исправить?
Я бы хотел получить вашу помощь.