Mike
Mike

Reputation: 136

Refresh Google Token using azure-mobile-apps with Cordova

How do I refresh a Google Token using azure-mobile-apps with Cordova?

From what I understand I need to use ‘access_type=offline’ or '/.auth/refresh'. However, I’m not sure how to implement these. Below is my attempt which returns 403 – Forbidden:

//var url = AzureDbSvc.client.applicationUrl + '/.auth/me'; /* Note - This Works */
var url = AzureDbSvc.client.applicationUrl + '/.auth/refresh'; 

var headers = new Headers();
headers.append('X-ZUMO-AUTH', AzureDbSvc.client.currentUser.mobileServiceAuthenticationToken);
fetch(url, { headers: headers })
.then(function (data) {
    console.log(data.status + ' : ' + data.statusText); //<<-- 403 : Forbidden
    ...
});

Any help is greatly appreciated!

Upvotes: 1

Views: 161

Answers (2)

Mike
Mike

Reputation: 136

Thanks Chris! The below works but please let me know if you see any issues or if it can be improved upon...

// Initial google login
AzureDbSvc.client.loginWithOptions('google', { parameters: { access_type: 'offline' } })
    .then(function (token) {...});


// Refresh token request
var headers = new Headers();
headers.append('X-ZUMO-AUTH',
   AzureDbSvc.client.currentUser.mobileServiceAuthenticationToken);

fetch(url, { headers: headers })
    .then(function (data) {
       console.log(data.status + ' : ' + data.statusText); //<<== 200 : OK
       var user_json = data.json();
       console.log('user_json:' + user_json);
       return user_json;
}).then(function (refreshToken) {...});

Upvotes: 1

Chris Gillum
Chris Gillum

Reputation: 15042

Do you see a refresh token when you call /.auth/me? A refresh token must be present in order for the /.auth/refresh API to work correctly.

BTW, you can see the details of these kinds of errors by enabling application logging. More details here: https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-enable-diagnostic-log. Let us know what those details are and we can help figure out how to make the scenario work.

Upvotes: 2

Related Questions