Search code examples
oauth-2.0google-oauthgoogle-calendar-api

google calendar api token expires in 7 days


I used this guide to create a Google calendar OAuth2 client id and client secret. I then used this gcalcli command to create an API access token:

gcalcli --noauth_local_webserver --client-id=$clientid --client-secret=$clientsecret --config-folder $path/auth agenda

This worked fine, and the command showed me a list of google calendar events.

It also created this token $path/auth/oauth which allows me to run the command again without having to authenticate every time, since the saved token does that for me.

My problem is that for some reason the oauth token expires every 7 days, and then I have to go through the same process again, to create a new token.

There's an entry in the oauth file showing "expires_in": 83, whereas other times I went through the same process the entry was "expires_in": 3599,. I'm assuming this entry defines the longevity of the token but I'm not 100% sure.

So my question is, why this token expires in a week, and what can I do to extend it for a longer period of time?

Additional information: This is basically a bash script running on Debian 11. It runs gcalcli, gets a list of google calendar events and sends reminders via sms.

I realise theres a way to use the refresh token to renew the access token, but it's not something I got around doing yet.


Solution

  • To be clear Access tokens expire within an hour, Refresh tokens are used to request a new access token when the refresh token has expired.

    If you are seeing your app stopping working after seven days its due to the following. You are probably getting an invalid token error.

    If you check the documentation for expiration it states

    A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.

    The solution is to set your app to production

    enter image description here

    I realize there's a way to use the refresh token to renew the access token, but it's not something I got around doing yet.

    If you are seeing your code stop working after seven days something in your code is using the refresh token already. I did a bit of digging in gcalcli internally it uses the Google api python client library. This is what is refreshing your access token. #L132