我正在尝试刷新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致命错误:在/var/www/drive/vendor/google/apiclient/src/Google/Client.php中,带有消息“刷新令牌的未捕获异常'LogicException”必须传入或设置为setAccessToken的一部分: 266 \ nStack跟踪:\ 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 ,但这不是主意。
我该如何解决?
我想得到您的帮助。
答案 0 :(得分:0)
最后令牌刷新工作正常,我不得不在 oauth2callback.php 中添加此行$client->setAccessType("offline");
,还删除并生成一个新的 client_id.json 以避免错误。
<?php
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('new_client_id.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/resp.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY
$client->setAccessType("offline");
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'] . '/drive';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>