Google Sheet API 访问令牌即将过期
Google Sheet API Access Token Getting Expired
我是 Google API 的新手,正在尝试遵循快速入门指南。我已经配置了我的 PHP 文件,如下所示
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('ddc.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$client->setApprovalPrompt('force');
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} 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));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
并使用 For Update Sheet 如下所示
$client = getClient();
$service = new Google_Service_Sheets($client);
在 ENABLE API 期间,我选择 https://example.com/sheet/oauth2callback
作为回调 URL 但此目录中没有任何相关文件,如 oauth2callback ,可能是那个问题?
我正在使用 CMD 在我的 Window 10 PC 中生成令牌并生成令牌。然后我在我的托管中使用该令牌,运行 我在浏览器中的文件。它工作正常但大约一个小时后令牌过期并再次要求我生成带有消息的令牌
Open the following link in your browser:.....
我已经读到它会自动刷新令牌,我们不需要做任何事情,但在我的情况下,它不会自动获取新令牌,或者可能是我遗漏了一些东西。让我知道这里是否有人可以帮助我。
访问令牌是短期令牌,它们将在一个小时后过期,然后用户将需要重新进行身份验证,或者您应该请求离线访问并将刷新令牌存储在您的代码中,以便您将来可以使用它在您当前的访问令牌到期时请求新的访问令牌。
假设您有一个刷新令牌并且它已在您的客户端上设置,下面的代码将根据需要刷新。
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
中窃取的代码
我是 Google API 的新手,正在尝试遵循快速入门指南。我已经配置了我的 PHP 文件,如下所示
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('ddc.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$client->setApprovalPrompt('force');
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} 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));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
并使用 For Update Sheet 如下所示
$client = getClient();
$service = new Google_Service_Sheets($client);
在 ENABLE API 期间,我选择 https://example.com/sheet/oauth2callback
作为回调 URL 但此目录中没有任何相关文件,如 oauth2callback ,可能是那个问题?
我正在使用 CMD 在我的 Window 10 PC 中生成令牌并生成令牌。然后我在我的托管中使用该令牌,运行 我在浏览器中的文件。它工作正常但大约一个小时后令牌过期并再次要求我生成带有消息的令牌
Open the following link in your browser:.....
我已经读到它会自动刷新令牌,我们不需要做任何事情,但在我的情况下,它不会自动获取新令牌,或者可能是我遗漏了一些东西。让我知道这里是否有人可以帮助我。
访问令牌是短期令牌,它们将在一个小时后过期,然后用户将需要重新进行身份验证,或者您应该请求离线访问并将刷新令牌存储在您的代码中,以便您将来可以使用它在您当前的访问令牌到期时请求新的访问令牌。
假设您有一个刷新令牌并且它已在您的客户端上设置,下面的代码将根据需要刷新。
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
中窃取的代码