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

Posted

技术标签:

【中文标题】Google drive api刷新令牌问题[重复]【英文标题】:Google drive api refresh token issue [duplicate] 【发布时间】:2019-02-16 22:45:42 【问题描述】:

我正在使用 google drive API 来创建电子表格。第一次使用我的 gmail 帐户进行 API 身份验证时,它会返回以下信息:


"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:267 中带有消息“必须传入或设置为 setAccessToken 的一部分”的未捕获异常“LogicException”

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

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));


任何机构请帮我解决问题。谢谢

【问题讨论】:

您好,请参考以下链接,***.com/questions/9241213/… 对您有帮助。 【参考方案1】:
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));


【讨论】:

嗨@Raja C,这段代码什么都不返回( $accessToken = $client->setAccessToken($accessToken); $decoded_accessToken= json_decode($accessToken); print_r($decoded_accessToken); )

以上是关于Google drive api刷新令牌问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Google Drive api范围和文件访问(驱动器vs drive.files)

关于google API中访问令牌和刷新令牌的问题

Google OAuth - 访问令牌和刷新令牌之间的区别 [重复]

访问令牌 Google API 的刷新令牌:R 代码

请求令牌时,Google API 刷新令牌为无

使用 OAuth 刷新令牌获取新的访问令牌 - Google API