google-apps-scriptgoogle-oauthgsuite-addons

How can I obtain an OAuth refresh token for a G Suite Add-on running on Apps Script?


Does anyone know how to obtain a refresh token for a G Suite Add on, considering that the authorization flow to obtain the user consent was handled by Apps Script?

I know how to obtain the access token with the method

ScriptApp.getOAuthToken()

But I don't know how to obtain the refresh token for a particular user.

Context:

I have an add-on that allows users to schedule recurring meetings. The user grants the app access to their calendar when they install the add-on from the G Suite Marketplace, so I can read and write on their calendars when they are running the script.

I want to send them email reminders a day before a meeting, so I want to read their calendars in the background using a cloud function when they are not running the script.

In order to do that I would need to have the refresh token for that user since the access token expires.

I'm really stuck on this for many days now. Any help would be much appreciated!

Thanks a lot.


Solution

  • You have two options:

    If you set access to offline (.setParam('access_type', 'offline')) it is enough if the user grants you permission once.