如何修复发生的错误:(400)刷新OAuth2令牌时出错,消息:';{“error”:“invalid”u grant}&“x27”;使用googledrivephpapi

如何修复发生的错误:(400)刷新OAuth2令牌时出错,消息:';{“error”:“invalid”u grant}&“x27”;使用googledrivephpapi,php,google-drive-api,google-oauth,Php,Google Drive Api,Google Oauth,我试图在我的项目中实现GoogleDriveAPI。关于刷新我的代币,我陷入了困境 我期望:发生错误:(400)刷新OAuth2令牌时出错,消息:“{”error:“invalid_grant”}”error 我的问题是它不允许我刷新我的令牌 以下是完整的代码: <?php session_start(); require_once('DBConn.php'); require('google-api-php-client/src/Google_Client.php'); requir

我试图在我的项目中实现GoogleDriveAPI。关于刷新我的代币,我陷入了困境

我期望:
发生错误:(400)刷新OAuth2令牌时出错,消息:“{”error:“invalid_grant”}”
error

我的问题是它不允许我刷新我的令牌

以下是完整的代码:

<?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;
    } 
}
?>
这是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);
    }
  }

如果你仔细看,你会说这个问题被问了很多次。你应该看看答案,看看是否有任何一个(如时钟漂移)是你问题的原因。我之前已经检查了所有可能的解决方案。之后我把我的贴在这里。因此,请检查和编码,并张贴解决方案,如果你有。谢谢。该应用程序已经在我的一个项目中运行。现在,我正在为另一个项目实现该应用程序,但它没有这样做。在5个左右的“无效授权”的可能原因中,您排除了哪些?可能值得在您的问题中发布http请求和响应。如果当前RT出现问题,还将生成新的刷新令牌
    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);
    }
  }