Google-api-php刷新令牌返回invalid_grant [英] Google-api-php Refresh Token returns invalid_grant
问题描述
我几乎搜索了谷歌第一页的每一个结果。但似乎无法找到答案。我正在使用Google API的refresh_token并收到:
刷新OAuth2令牌时出错,消息:'{error :invalid_grant}
我在做什么。首先:我创建并存储了一个持久连接到google 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();
$ b $ if($ client-> getAccessToken()){
$ jsonarray = json_decode($ client-> getAccessToken());
$ arrGoogleAuth ['access_token'] = $ jsonarray-> access_token;
$ arrGoogleAuth ['refresh_token'] = $ jsonarray-> refresh_token;
//文件写入
$ myFile =refreshtoken.conf;
$ fh = fopen($ myFile,'w')或死(无法打开文件);
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');
$ b $ createdFile = $ service-> files-> insert($ file,array(
'data'=> $ data,
'mimeType'=> 'text / plain',
));
print_r($ createdFile);
* /
//访问令牌可能已被懒惰地更新。
$ _SESSION ['token'] = $ client-> getAccessToken();
} else {
$ auth = $ client-> createAuthUrl();
header(Location:$ auth);
$ b 基本上所有的东西都运行,令牌被存储在一个文本文件中:
{
access_token:xxxxxxxxxxxxxxxN4U0ys2wy5monxs0Xh5fu5ayKL0OIENo-d1sN6g3YA,
token_type:承载者,
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令牌的错误消息:' {error:invalid_grant}
解决方案 在验证之前,一定有这样的东西: b
$ client-> grantType(refresh_token)
I've nearly searched every result of the first page of google for this. But can't seem to find the answer. I'm working with a refresh_token by Google's API and receiving:
Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }
What i'm doing. First: i'm creating and storing a persistant connection to the google 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");
}
So basicly everything runs and the token gets stored in a textfile:
{
"access_token":"xxxxxxxxxxxxxxxN4U0ys2wy5monxs0Xh5fu5ayKL0OIENo-d1sN6g3YA",
"token_type":"Bearer",
"expires_in":3600,
"refresh_token":"xxxxxxxxxxxxDON-l90BNUcJgnkfZWDfg",
"created":1358120143
}
When i'm trying to auth using the following code:
$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();
I'm getting the following error:
Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }
解决方案 Before Authenticate, there must be something like:
$client->grantType("refresh_token")
这篇关于Google-api-php刷新令牌返回invalid_grant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!