Как исправить Произошла ошибка: (400) Ошибка обновления токена OAuth2, сообщение: ‘{& quot; error & quot; : & quot; invalid_grant & quot; } ‘с помощью Google Drive PHP API

Я пытаюсь реализовать 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);
}
}

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]