OAuth refreshToken

Questions and answers on developing, deploying and using plugins and JavaBeans

OAuth refreshToken

Postby rafig » Fri Apr 23, 2021 1:56 pm

Hi all,
(although this is probably best answered by Patrick Ruhsert...)
Years ago I used Patrick's 'Google2' plugin to do some cool OAuth stuff with Google, specifically I could use the "refreshToken" I got when authorising a service.
At the time I was using it to access Google Analytics, but now I want to use gMail.
I would store this in my database and when I wanted to access the service again, I would just use code like this
Code: Select all
      if (ga_rec.ga_refresh_token) {
         status_msg('Current ga_refresh_token ' + ga_rec.ga_refresh_token);
         ga_rec.ga_access_token = plugins.Google2.getAccessToken(ga_rec.ga_refresh_token);
         status_msg('Access Token = ' + ga_rec.ga_access_token);
      } else {
         // get new refresh token
         ga_rec.ga_refresh_token = plugins.Google2.authorize(ga_rec.ga_auth_code, scopes.globals.ga_client_id, scopes.globals.ga_scopes);
         status_msg('New Refresh Token = ' + ga_rec.ga_refresh_token);
      }

I can see that there is a reference in the Wiki to a refreshToken
https://wiki.servoy.com/display/DOCS/OAuthService#OAuthService-refreshToken()
I have tried using this, and it kind of works if the 'service' that I have built is still valid, it will send an email with a new accessToken from
Code: Select all
auth_gmail_token = gmail_service.refreshToken();

But I can't see where/how I pass in the previously stored token to the 'service' if this is a new session??
I would expect a call like
Code: Select all
auth_gmail_token = gmail_service.refreshToken(my_stored_token);

I tried
Code: Select all
gmail_service.setAccessToken(my_stored_token);
auth_gmail_token = gmail_service.refreshToken();

but that threw an error & didn't work.

[my analysis of the service build process is
1) new session, so run 'connect' code
Code: Select all
plugins.oauth.serviceBuilder(auth_clientId)
.clientSecret(auth_clientSecret)
.callback(scopes.auth.gmail_deeplink, 30)
.scope('https://mail.google.com/')
.build(plugins.oauth.OAuthProviders.GOOGLE);

2) this makes web page appear for you to choose your gmail account to authorise
3) once 'allowed', callback method is called
4) if 'success', get 'service' and get accessToken from it and then one can email
Code: Select all
function gmail_deeplink(result, auth_outcome) {
   if (result) {
      //SUCCESS
      gmail_service = auth_outcome;
      if ( my_stored_token != null ) {
         //gmail_service.setAccessToken(my_stored_token);
         auth_gmail_token = gmail_service.refreshToken();
      } else {
         // no stored token
         auth_gmail_token = gmail_service.getAccessToken();
         my_stored_token = gmail_service.getRefreshToken();
      }

5) if the 'connect' method is called again, the deep link method is immediately fired and fails as 'not success' is passed
6) if I add code to 'connect' method to see if 'service' already exists and then I try & fire deep link method passing "true" & "service", it fails when I try to refreshToken (as I can't pass it my valid, stored previous refreshToken to get new accessToken...
/end analysis]

So, please, can someone clarify how to correctly use 'refreshToken' so I don't have to keep asking user to allow access??
Thanks,
Rafi
Servoy Certified Developer
Image
rafig
 
Posts: 704
Joined: Mon Dec 22, 2003 12:58 pm
Location: Watford, UK

Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 12 guests