我幾乎搜索了谷歌第一頁的每一個結果。但似乎無法找到答案。我正在使用谷歌API的refresh_token,並收到:Google-api-php刷新令牌返回invalid_grant
Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }
我在做什麼。第一:我創建並存儲到谷歌API的一個持續性連接:
$client = new Google_Client();
$client->setClientId('xxxxxx-s73q0pa41aq3i2kcpatmpas6e6kkp99h.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxx');
$client->setRedirectUri('http://xxxxxxxx/generaterefreshtoken.php');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
$jsonarray = json_decode($client->getAccessToken());
$arrGoogleAuth['access_token']=$jsonarray->access_token;
$arrGoogleAuth['refresh_token']=$jsonarray->refresh_token;
//filewrite
$myFile = "refreshtoken.conf";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $client->getAccessToken());
fclose($fh);
/*
die();
$service = new Google_DriveService($client);
$file = new Google_DriveFile();
$file->setTitle('My document.txt');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
print_r($createdFile);
*/
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
$auth = $client->createAuthUrl();
header("Location: $auth");
}
所以basicly一切運行和令牌的形式存儲在文本文件:
{
"access_token":"xxxxxxxxxxxxxxxN4U0ys2wy5monxs0Xh5fu5ayKL0OIENo-d1sN6g3YA",
"token_type":"Bearer",
"expires_in":3600,
"refresh_token":"xxxxxxxxxxxxDON-l90BNUcJgnkfZWDfg",
"created":1358120143
}
當我試圖權威性使用下面的代碼:
$client = new Google_Client();
$client->setClientId($googleDriveConfig['clientid']);
$client->setClientSecret($googleDriveConfig['clientsecret']);
$client->setRedirectUri(curPageURL);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->refreshToken(file_get_contents('../_config/refreshtoken.conf'));
$client->authenticate();
,我發現了以下錯誤: 錯誤刷新的OAuth2令牌,消息:「{ 「錯誤」: 「invalid_grant」}
grantType不是Google_Client上的函數,有一個refreshToken函數,但它也不起作用。 – Mikushi
如果grantType不是Google_Client類中的實際函數,爲什麼選擇此選項爲正確? – EHerman
對於任何想使用刷新令牌刷新訪問令牌的人,您可以執行'''$ client-> refreshToken($ refresh_token); $ _SESSION ['token'] = $ client-> getAccessToken();如果您嘗試刷新除最近被授予的刷新令牌之外的其他東西,則會返回invalid_grant。 – EHerman