12
Views
1
Comments
Restful API Refresh Bearer Token
Question

Hi 

I'm trying to refresh the bearer token for an Restful APi I'm trying to connect to. I've set up a 'onBeforeRequest' event so that I can customise the header. The resful api documentation has advise me to run the following code to refresh my token.

function refreshTokenLogin(refreshToken) {

var args = {

client_id: "clientid",grant_type: "refresh_token",scope: "scope",refresh_token: refreshToken

};

var xhr = $.ajax({

url: 'alemba.web/oauth/login',
type: 'POST',
data: args,contentType: 'application/x-www-form-urlencoded'

});

xhr.done(onGrantSuccess).fail(function (err) {

if (err.status == 401) {

}

else {

onGrantFailure(err, "refresh_token");

}

});

return xhr;

here is my logic


How do I apply the access token from the javascript code they've given me?

Thanks


mvp_badge
MVP
Rank: #2

That's difficult, I think. You cannot really handle that very well in the OnBeforeRequest, as you need another REST call to refresh the token, but then you'd need to recall the original call, and there's just no way to do that. I think the best way to handle is to abstract all your REST methods behind Server Actions or Service Actions, then handle the Exception or status code, then refresh, then recall the action.