0

Having a problem with the refreshToken procedure with Google API, upon an expired token is experienced the refreshToken doesn't get an AccessToken. I've already saved the refresh token to the db at this point.

$client = new Google_Client();
$client->setApplicationName("APP NAME");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setAccessType('offline'); 
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
$client->setAccessToken( json_encode($access_token_from_db) );

if ( $client->isAccessTokenExpired() ) :
    $client->refreshToken($refresh_token_from_db);
    $new_access_token = $client->getAccessToken();
    print_r($access_token);
    //save to db -- but this fails
endif;

After running this the new access token is still empty. Even if I setAccessToken to the now expired token prior to checking expiration the same problem persists.

There doesn't seem to be a straight forward example on a state after a refreshToken has already been saved.

In my application we would cron job to send updates to a user's calendar based on actions (if necessary to understand the application purpose).

The reason this answer doesn't seem correct to me: How to refresh token with Google API client?

is because it's not using the $client->isAccessTokenExpired() method, plus isn't a clean solution, the script should be very minimal and small to complete the task. In theory:

  • setAccessToken to the one given on authorization
  • if expired (via function), use the refresh to access a new token
  • save the token and continue

While the above example had similar functions it's a 2013 answer and may work but isn't the best way to do this I believe.

Community
  • 1
  • 1
Jay Kappa
  • 11
  • 4
  • Possible duplicate of [How to refresh token with Google API client?](http://stackoverflow.com/questions/9241213/how-to-refresh-token-with-google-api-client) – DaImTo May 11 '17 at 06:32
  • That answer is outdated and using timing instead of the isAccessTokenExpired method, and is a cluttered response, a cleaner solution / method should be possible. – Jay Kappa May 11 '17 at 14:11
  • The possible duplicate also thinks the refresh token expires but it's not supposed to. – Jay Kappa May 11 '17 at 14:27

1 Answers1

1

My failure was because I messed up my own clientID, but I'm posting this script here anyways because it took me forever to find the correct answer that was small, efficient and effective (the below is strictly after a token and refresh have been already saved to your db):

$client = new Google_Client();
$client->setApplicationName('APP NAME');
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setAccessType('offline'); 
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
// my app is for google calendar.

$access_token_from_db = 'XXXXXX';
$refresh_token_from_db = 'XXXXX';
$_tokenArray['access_token'] = $access_token_from_db

//must be set as json
$client->setAccessToken( json_encode($_tokenArray) );

//check if token expired:
if ( $client->isAccessTokenExpired() ) :
    $client->refreshToken($refresh_token_from_db);
    $new_access_token = $client->getAccessToken();

    //now save your new access token to your db

endif;
Jay Kappa
  • 11
  • 4