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
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
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
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
auth_gmail_token = gmail_service.refreshToken(my_stored_token);
I tried
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
- new session, so run ‘connect’ code
plugins.oauth.serviceBuilder(auth_clientId)
.clientSecret(auth_clientSecret)
.callback(scopes.auth.gmail_deeplink, 30)
.scope('https://mail.google.com/')
.build(plugins.oauth.OAuthProviders.GOOGLE);
- this makes web page appear for you to choose your gmail account to authorise
- once ‘allowed’, callback method is called
- if ‘success’, get ‘service’ and get accessToken from it and then one can email
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();
}
- if the ‘connect’ method is called again, the deep link method is immediately fired and fails as ‘not success’ is passed
- 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