我们如何在访问令牌过期后获取访问令牌或使用 PHP 的 google 文档撤销访问令牌?
我目前正在使用 PHP 代码开发 google docs API。我正在关注这个,但是在这种情况下,当我的令牌过期时,我需要删除token.json文件,并在终端再次运行PHP faststart.php命令生成新的token.json文件。通过这种方法,我能够继续我的工作。但这种方法并不合适。
我做了这样的代码 -
<?php
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Docs API PHP Quickstart');
$client->setScopes([
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive",
Google_Service_Drive::DRIVE_READONLY,
]);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('token.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($accessToken);
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
从上面的代码中,我无法从我的文件中获取刷新令牌。那为什么要用这个函数呢!
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($accessToken);
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
虽然我做了很多研究并浏览了这个链接- Google API客户端“刷新令牌必须传入或设置为 setAccessToken 的一部分”
但是,我没有得到我想要的。谁能告诉我在从文件中获取刷新令牌时缺少什么或做错了什么?它也不会向我们提供任何类型的令牌已过期的通知,它只是简单地抛出错误。我无法在我的项目中执行此操作。
请告诉我我们如何才能实现这一目标。这将是一个很大的帮助。
I am currently working on google docs API with PHP code. I am following this but with this case When my token get expires, I need to delete the token.json file and again run the PHP quickstart.php command on the terminal to generate a new token.json file. By this method, I am able to continue with my work. But this method is not appropriate.
I did code like this-
<?php
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Docs API PHP Quickstart');
$client->setScopes([
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive",
Google_Service_Drive::DRIVE_READONLY,
]);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('token.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($accessToken);
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
from this above code, I am not able to get the refresh token from my file. Then why this function is used!
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($accessToken);
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
Although I researched a lot and gone through this links-
Google API Client "refresh token must be passed in or set as part of setAccessToken"
But, I didn't get what I wanted. Can anybody tell me what am I missing or doing wrong in fetching the refresh token from the file? It also won't give us any kind of notification that the token is expired, It just simply throws an error. I cannot do this in my project.
Please tell me how we can achieve this. It would be a great help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论