Google Drive api刷新令牌问题[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在使用谷歌驱动API来创建电子表格。 API首次使用我的Gmail帐户进行身份验证时,它会返回以下信息:

{
"access_token":"XXXXXXXXXs7ZqxIslxBnkLXXXXXJOvMjULXXXXXXXXXXX",
"expires_in":3600,
"scope":"https:\/\/www.googleapis.com\/auth\/spreadsheets",
"token_type":"Bearer",
"created":1536680902
}

基本上,它允许我在访问令牌到期后仅使用访问令牌1小时,当我尝试刷新令牌时,它给我下面的错误:

致命错误:在C:\ xampp \ htdocs \ drivegoogle \ src \ Google \ Client.php中,带有消息'刷新令牌的未捕获异常'LogicException'必须传入或设置为setAccessToken'的一部分:267

以下是我正在运行以获取访问权限和刷新令牌的代码:

function getClient() {


$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
// $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
$client->setAuthConfig('credentials.json');
$client->setRedirectUri("http://localhost/drivegoogle/index.php");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// Load previously authorized credentials from a file.
if (file_exists('1536680902.json')) {
$accessToken = json_decode(file_get_contents('1536680902.json'),
true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

if (isset($_GET['code'])) {
$authCode = $_GET['code'];
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
header('Location: ' . filter_var($this->redirectUri,
FILTER_SANITIZE_URL));
if(!file_exists(dirname('1536680902.json'))) {
mkdir(dirname('1536680902.json'), 0700, true);
}

file_put_contents($this->tokenFile, json_encode($accessToken));
}else{
exit('No code found');
}
}

$client->setAccessToken($accessToken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {

// save refresh token to some variable

$refreshTokenSaved = $client->getRefreshToken();
// update access token
$client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);

// pass access token to some variable
$accessTokenUpdated = $client->getAccessToken();

// append refresh token
$accessTokenUpdated['refresh_token'] = $refreshTokenSaved;

//Set the new acces token
$accessToken = $refreshTokenSaved;
$client->setAccessToken($accessToken);

// save to file
file_put_contents($this->tokenFile,
json_encode($accessTokenUpdated));
}
}

任何身体请帮我解决问题。谢谢

php google-api google-sheets-api
1个回答
0
投票
Try with Below Code,

function getClient() {

$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
// $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
$client->setAuthConfig('credentials.json');
$client->setRedirectUri("http://localhost/drivegoogle/index.php");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// Load previously authorized credentials from a file.
if (file_exists('1536680902.json')) {
$accessToken = json_decode(file_get_contents('1536680902.json'),
true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

if (isset($_GET['code'])) {
$authCode = $_GET['code'];
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
header('Location: ' . filter_var($this->redirectUri,
FILTER_SANITIZE_URL));
if(!file_exists(dirname('1536680902.json'))) {
mkdir(dirname('1536680902.json'), 0700, true);
}

file_put_contents($this->tokenFile, json_encode($accessToken));
}else{
exit('No code found');
}
}

$accessToken = $client->setAccessToken($accessToken);
$decoded_accessToken= json_decode($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {

// save refresh token to some variable

$refreshTokenSaved = $client->getRefreshToken($decoded_accessToken->refresh_token);
// update access token
$client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);

// pass access token to some variable
$accessTokenUpdated = $client->getAccessToken();

// append refresh token
$accessTokenUpdated['refresh_token'] = $refreshTokenSaved;

//Set the new acces token
$accessToken = $refreshTokenSaved;
$client->setAccessToken($accessToken);

// save to file
file_put_contents($this->tokenFile,
json_encode($accessTokenUpdated));
}
}
© www.soinside.com 2019 - 2024. All rights reserved.