Refresh token is not returned – Google API

Posted by

On the premises, refresh token will only be returned if the access parameter is set to ‘offline’.

Problem

It worked on my local environment but did not seem to return refresh token on a staging environment with Heroku.

As I delved into this issue, I found out that clients will receive refresh token only once for authorization. On my local environment, it didn’t seem like the case, which was really confusing.

Also, I found out that clients have to deauthorize the application from their google account then the API will send a new refresh token the next time they authorize. So, probably on my local environment, my google account I signed with deauthorized the application every after a request to the API. On the other hand, the application kept being authorized on Heroku environment.

Solution

Set the prompt parameter to ‘consent’ so that the application asks for reauthorization from google accounts for every API request. That way, refresh token will be returned every time users reauthorize the application.

  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',
    prompt: 'consent',
    state: redirectUri,
    scope: SCOPES
  });

Thanks for reading.

Hope you enjoyed the article. If you have any question or opinion to share, feel free to write some comments.

Facebook Comments