Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Refreshing expired tokens under OAuth 2.0

Hello,

 

I'm writing for some assistance in automatically refreshing expired tokens under Fitbit Web API.  The initial user login is successful, but after this the tokens expire and I've been making several attempts to create a function to refresh the tokens, but with no avail.  I've looked at solutions on this forum and the Using OAuth page, but nothing works for me.  I don't want the user to constantly login the setting menu each time the information needs updating.

This is what I have so far.

function refreshTokens() {
   console.log("Refreshing tokens");
const accessToken = settingsStorage.getItem("oauth").access_token; 
const refreshToken = settingsStorage.getItem("oauth").refresh_token; 
  
  fetch(`https://api.fitbit.com/oauth2/token?grant_type=refresh_token&refresh_token=${refreshToken}`, {
    method: "POST",
    headers: {
      "Authorization": "Basic [Base64 encoded]",
      "Content-Type": "application/x-www-form-urlencoded"
    }
  })
  .then(function(res) {
    return res.json();
  })
  .then(function(data) { 
      if (!("errors" in data)) {
        accessToken = data.access_token;
        refreshToken = data.refresh_token;
      }
      else {
        // Error handling?
        console.log("Error: ", data);
        return this.refreshTokens().then(data => {
                debug("Token refreshed");})
      }
  })
  .catch(function(error) {
      console.log("Error: Refreshing tokens failed:", error);
  });
}

I get these errors:

Uncaught non-error: null

and

Error:  { errors: 
   [ { errorType: 'invalid_grant',
       message: 'Refresh token invalid: undefined. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process.' } ],
  success: false }

I also did a console.log for the oauth key and it came up undefined.  The only way things work is to manually login again through the settings menu - but the whole point is that's when the function should kick in to renew the tokens, surely?

 

Any help would be appreciated at this point, as I don't know what else to do.

 

Best Answer
0 Votes
4 REPLIES 4

please check for invalid_grant reasons https://tools.ietf.org/html/rfc6749#section-5.2

Best Answer
0 Votes

Yes, the invalid grant reason is that the token is expired.  But how to automatically renew it once expired?

Best Answer
0 Votes

Hi @bsavarin 

 

The information you're providing seems correct.   However, the parameters should be body parameters, not query parameters.   Have you tried that to see if it helps?

 

Gordon

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

Change

 

const refreshToken = settingsStorage.getItem("oauth").refresh_token; 

 

 for

 

const refreshToken = JSON.parse(settingsStorage.getItem("oauth")).refresh_token; 

 

 

You ended up providing "undefined" as refresh_token because everything in the Settings Storage is a string. You need to convert settingsStorage.getItem("oauth") into a JSON object using JSON.parse(...) before reading its properties.

 

Best Answer
0 Votes