Search code examples
phpoauth-2.0google-apigoogle-calendar-apigoogle-oauth

How to have server access Google Calendar API?


I'm trying to build a web app that will access a user's Google Calendar API without them actually being there. So basically add or modify events in their Google Calendar from my server (PHP script) in the background.

I am already oauth2 from Google Calendar and I have a refresh token for them so they don't have to re-login each time. They just log into oauth once and they are good. But how can I make it so that the server can make changes in the background?


Solution

  • I figured it out. So what I did was I stored the access_token in the database and checked if the access token was expired. If it was, I would use the refresh_token in the database to generate a new access token. So every time the server would access the user's account, it would use the access token from the database. Also, make sure to set the access type to offline like so:

    $client->setAccessType('offline');