Pergunta

I'm creating a server app for Google Calendar using OAuth. Right now it initially works fine, the problem arises when the access token expires and is unable to renew its self. I thought I had that covered with the isAccessTokenExpired() clause...

$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));   

if (isset($_SESSION['token'])) {$client->setAccessToken($_SESSION['token']);}

/* GOOGLE OAUTH  *////-----------------------------------------
$pkey = file_get_contents($pkey_file);
$cred = new Google_Auth_AssertionCredentials(
  $service_acct,
  array('https://www.googleapis.com/auth/calendar'),
  $pkey );

$cred->sub = "xxxxxxxxxx@x"; 
$client->setAssertionCredentials($cred);

if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
   }
$_SESSION['token'] = $client->getAccessToken(); 
/*  ------   *////---------------------------------------------
 $cal = new Google_Service_Calendar($client);
Foi útil?

Solução

I just solved a similar problem by forcing to not use the cache in Google_Auth_AssertionCredentials. I guess that while trying and errors the cache loads

Change your $cred declaration to this:

$cred = new Google_Auth_AssertionCredentials($service_acct, array('https://www.googleapis.com/auth/calendar'), $pkey, 'notasecret','http://oauth.net/grant_type/jwt/1.0/bearer',false,false
);

The last false disable the use of a cache.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top