How to fix An error occurred: (400) Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' using Google Drive PHP API -



How to fix An error occurred: (400) Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' using Google Drive PHP API -

i trying implement google drive api in project. stuck @ point regarding refreshing token.

i expecting: an error occurred: (400) error refreshing oauth2 token, message: '{ "error" : "invalid_grant" }' error.

my problem not allow me refresh token.

here finish code:

<?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); homecoming 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); seek { $client->refreshtoken($token->refresh_token); } grab (exception $e) { print "an error occurred: (" . $e->getcode() . ") " . $e->getmessage() . "\n"; } $newtoken = $client->getaccesstoken(); homecoming $newtoken; } } ?>

and code here token refresh part:

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); seek { $client->refreshtoken($token->refresh_token); } grab (exception $e) { print "an error occurred: (" . $e->getcode() . ") " . $e->getmessage() . "\n"; } $newtoken = $client->getaccesstoken(); homecoming $newtoken; }

and code of 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 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 oauth2 token, message: '$body'", $code); } }

php google-drive-sdk google-oauth

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -