Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting refresh tokens from Google with OAuth.io

I am trying to get access tokens from OAuth.io for any Google based provider however whenever I authenticate I get an access_token but no refresh_token. I have chosen offline for the access_type but still no joy.

I have tried looking through the documentation for a solution but it barely covers anything related to the refresh token.

like image 947
willjcksn Avatar asked May 20 '14 11:05

willjcksn


1 Answers

To get the refresh token from Google, you need 2 things:

  • The offline option cf https://developers.google.com/accounts/docs/OAuth2WebServer

    "A token that may be used to obtain a new access token. Refresh tokens are valid until the user revokes access. This field is only present if access_type=offline is included in the authorization code request."

  • The option approval_prompt set to "force" cf https://developers.google.com/accounts/docs/OAuth2WebServer

    "Important: When your application receives a refresh token, it is important to store that refresh token for future use. If your application loses the refresh token, it will have to re-prompt the user for consent before obtaining another refresh token. If you need to re-prompt the user for consent, include the approval_prompt parameter in the authorization code request, and set the value to force."

so your script should look something like

OAuth.popup('google', {
   authorize: {
      approval_prompt: 'force'
   }
}).then(function(google) {
   console.log(google.refresh_token)
   //send the refresh token to your server
})

If you are working client-side (Javascript / iOS / Android / Phonegap), you may also need to activate the following option: Send refresh token to front-end in the OAuth.io dashboard > General > advanced option to allow your client side SDK to retrieve the refresh token

https://jsfiddle.net/Lqyc5jpw/

like image 86
Thibaud Arnault Avatar answered Sep 28 '22 19:09

Thibaud Arnault