php - Google API oauth2 token expires? -
i have problem google api , oauth2 token.
there app allows synchronize contacts / calendar google account oauth2 token.
when first time user wants connect google account, needs grant access , app receiving code/token saved , used offline synchronization later.
function getclient($app) { $client = new google_client(); $client->setauthconfig("path_to_secret.json"); switch($app) { case 'contacts': $client->addscope(google_service_script::www_google_com_m8_feeds); $client->addscope(google_service_people::userinfo_email); break; case 'calendar': $client->addscope(google_service_calendar::calendar); break; default: throw new exception('api callback not defined in setup'); } $client->setaccesstype('offline'); // offline access $client->setincludegrantedscopes(true); // incremental auth $client->setredirecturi(google_app_url . $app.'/callback.php'); return $client; }
(there different tokens contacts , calendar)
the synchronization script:
... try { $client = getclient('calendar'); $client->setaccesstoken(unserialize($accesstoken)); $http = $client->authorize(); $service = new google_service_calendar($client); ... }
$accesstoken serialized string like:
a:5:{s:12:"access_token";s:131:"******token_here********";s:10:"token_type";s:6:"bearer";s:10:"expires_in";i:3598;s:8:"id_token";s:902:"***id_token****";s:7:"created";i:1505178047;}
this working first time , couple more times after time(hours) there error:
error: {"error": { "errors": [ { "domain": "global", "reason": "autherror", "message": "invalid credentials", "locationtype": "header", "location": "authorization" } ], "code": 401, "message": "invalid credentials" }}
what doing wrong?
what interesting contacts synchronization works fine time (access token has same attributes in calendar synchronization )
ok, propably solved - refresh_token provided first time only, when testing more times didn't refresh token. when revoked access in https://myaccount.google.com/u/0/permissions , connected again received refresh token. assume work properly
Comments
Post a Comment