我正在尝试在我的项目中实施Google Drive API。关于刷新我的令牌,我陷入了困境。
我期待:An error occurred: (400) Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'
错误。
我的问题是它不允许我刷新我的令牌。
这是完整的代码:
<?php
session_start();
require_once('DBConn.php');
require('google-api-php-client/src/Google_Client.php');
require('google-api-php-client/src/contrib/Google_DriveService.php');
require('google-api-php-client/src/contrib/Google_Oauth2Service.php');
class ApiMethods extends DBConn
{
public $CLIENTID = "<somestring>-589hmda0tneueutdo1ouhtuus2qoc0td.apps.googleusercontent.com";
public $CLIENTSECRET = "<somestring>";
public $REDIRECTURL = "http://www.<somestring>.com/<somestring>/subscribe.php";
public $SCOPES = array('https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/drive.install');
public $CLIENTIDTEACHER = "<somestring>-mudd3kfb3bdggjfi19bot0itoaqi6iso.apps.googleusercontent.com";
public $CLIENTSECRETTEACHER = "<somestring>";
public $REDIRECTURLTEACHER = "http://www.<somestring>.com/<somestring>/index.php";
public $DEVELOPERKEYTEACHER = "<somestring>";
public $DRIVE_SCOPE1 = 'https://www.googleapis.com/auth/drive';
public $SERVICE_ACCOUNT_EMAIL = 'qwdwe-6gc2cjrlgit1jr3hc0mqlic557kqh5i5@developer.gserviceaccount.com';
public $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'http://www.<somestring>.com/<somestring>/classes/4f9cc06a1081cb5982736079f7c5f25c35aa8c37-privatekey.p12';
public function buildService($userEmail) {
$key = file_get_contents($this->SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$this->SERVICE_ACCOUNT_EMAIL,
array($this->DRIVE_SCOPE1),
$key);
$auth->sub = $userEmail;
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}
public function RefreshToken($token)
{
$client = new Google_Client();
$client->setClientId($this->CLIENTID);
$client->setClientSecret($this->CLIENTSECRET);
$client->setRedirectUri($this->REDIRECTURL);
$client->setScopes($this->SCOPES);
$token = json_decode($token);
try
{
$client->refreshToken($token->refresh_token);
}
catch (Exception $e)
{
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
}
$newtoken = $client->getAccessToken();
return $newtoken;
}
}
?>
从这段代码开始,这里是令牌刷新部分:
public function RefreshToken($token)
{
$client = new Google_Client();
$client->setClientId($this->CLIENTID);
$client->setClientSecret($this->CLIENTSECRET);
$client->setRedirectUri($this->REDIRECTURL);
$client->setScopes($this->SCOPES);
$token = json_decode($token);
try
{
$client->refreshToken($token->refresh_token);
}
catch (Exception $e)
{
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
}
$newtoken = $client->getAccessToken();
return $newtoken;
}
这是Google_OAuth2.php的代码:
public function refreshToken($refreshToken) {
$this->refreshTokenRequest(array(
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token'
));
}
private function refreshTokenRequest($params) {
$http = new Google_HttpRequest(self::OAUTH2_TOKEN_URI, 'POST', array(), $params);
$request = Google_Client::$io->makeRequest($http);
$code = $request->getResponseHttpCode();
$body = $request->getResponseBody();
if (200 == $code) {
$token = json_decode($body, true);
if ($token == null) {
throw new Google_AuthException("Could not json decode the access token");
}
if (! isset($token['access_token']) || ! isset($token['expires_in'])) {
throw new Google_AuthException("Invalid token format");
}
$this->token['access_token'] = $token['access_token'];
$this->token['expires_in'] = $token['expires_in'];
$this->token['created'] = time();
} else {
throw new Google_AuthException("Error refreshing the OAuth2 token, message: '$body'", $code);
}
}