2

I'm trying to use Google API (PHP) to perform a daily sync between a system calendar and the user's Google Calendars. I want a system user to be able to set up access to their calendar once from within the system, then the system can synchronise the calendars each day via a cron job.

I have managed to get the entire process working, from authorising the access to syncing the calendars, but I am getting stuck with the token expiry times.

The way I thought it worked in v3 was the old token would act as the refresh token. So, once a user had provided authorisation, I just need to pass the old token to Google and it would allow me to run the sync.

This seems to work, as long as I am within the 1 hour expiry time of the initial token. Once an hour passes, it stops working and I need to get the user to authorise access again.

Is there a way to get the user to authorise access once, then sync the calendars once per day without the user having to authorise access again?

Thanks!

thebronsonite
  • 99
  • 1
  • 10

1 Answers1

3

Well, during your authorization with Google, you will receive a token that will expire in 3600 seconds or one hour and it is normal to be expired. So you need to use refresh token to get a new working token.

Here are the steps that you need:

$token = $client->getAccessToken();
$authObj = json_decode($token);
if(isset($authObj->refresh_token)) {
save_refresh_token($authObj->refresh_token);
}

It is important to save this refresh_token, then you can update it with

$client->refreshToken($your_saved_refresh_token);

And then set your new access token to the session:

$_SESSION['access_token'] = $client->getAccessToken();

For more information, check this SO question.

Community
  • 1
  • 1
KENdi
  • 7,205
  • 2
  • 14
  • 26
  • Thanks, I just rechecked everything and found out the refresh token only appears the very first time you gain access. Thanks! – thebronsonite Nov 08 '16 at 10:02