I'd like to add a bit more info on this subject for those frustrated souls who encounter this issue. The key to getting a refresh token for an offline app is to make sure you are presenting the consent screen. The refresh_token
is only returned immediately after a user grants authorization by clicking "Allow".
The issue came up for me (and I suspect many others) after I'd been doing some testing in a development environment and therefore already authorized my application on a given account. I then moved to production and attempted to authenticate again using an account which was already authorized. In this case, the consent screen will not come up again and the api will not return a new refresh token. To make this work, you must force the consent screen to appear again by either:
prompt=consent
or
approval_prompt=force
Either one will work but you should not use both. As of 2021, I'd recommend using prompt=consent
since it replaces the older parameter approval_prompt
and in some api versions, the latter was actually broken (https://github.com/googleapis/oauth2client/issues/453). Also, prompt
is a space delimited list so you can set it as prompt=select_account%20consent
if you want both.
Of course you also need:
access_type=offline
Additional reading: