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

How to build Refresh Token Flow

Receiving accesstoken and refreshtoken <oauth> component in settings OnAccessToken. One of the api call is returning 401 because access token expired. How to invoke the refresh token call??? or any internal things happen in the fitbit??.. No examples found in the documentation. Please provide code snippet to retain the user state???.. There is no much documentation or examples on refresh token.

 

function getDataFromResourceServer() {
console.log('hello')
fetch(endpoint, {
method: 'GET',
headers:{
'Content-Type': 'application/json',
'Authorization': 'bearer ' +settingsStorage.getItem('token')
}} )
.then(function (response) {
// Here getting 401 response
console.log('response', JSON.stringify(response));
response.json()
.then(function(data) {
console.log('success ', data);

});
})
.catch(function (err) {
console.log("Error fetching : " + err);
});
}

Best Answer
0 Votes
4 REPLIES 4

Hi @Sandy111 

 

Please review this section of the documentation for refreshing access tokens: https://dev.fitbit.com/build/reference/web-api/oauth2/#refreshing-tokens.  The syntax is 

 

POST https://api.fitbit.com/oauth2/token
Authorization: Basic Y2xpZW50X2lkOmNsaWVudCBzZWNyZXQ=
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token&refresh_token=abcdef01234567890abcdef01234567890abcdef01234567890abcdef0123456

 

Best,

Gordon

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

   

I am building an app in fitbit watch. Under settings section below is my code snippet. From companion we are making api calls using fetch, access token is getting expired because it is short lived. Now how to call the token endpoint with the refresh token and get access token??

where should i call this??..  Should i call the oauth component or how??

<Oauth
settingsKey="oAuth1"
title= "Login"
label={label}
status={status}
authorizeUrl="https://blahblah.com/Authorize"
requestTokenUrl="https://blahblah.com/OAuth/Token"
clientId="watchdev"
clientSecret="watchXXXX"
scope="All"

onAccessToken={async (data) => {
console.log('heloooooooooooooooooooooo');
console.log(JSON.stringify(data));
label = "You are logged in as " + props.settingsStorage.getItem('userName');

}}

/>

Best Answer

Does anyone help??

Best Answer
0 Votes

HI @Sandy111 

 

After you determine the access token has expired, you should execute the endpoint as I described in the previous post by providing your current refresh token to obtain a new access token and refresh token.  

 

In your syntax, it looks like you're trying to use OAuth version 1.  This is not supported.  You should be using OAuth 2.0.   Please review the documentation for more details: https://dev.fitbit.com/build/reference/web-api/oauth2/.   If you would like to test the OAuth 2.0 flow and the refresh token flow, you can do so with the OAuth 2.0 tutorial: https://dev.fitbit.com/build/reference/web-api/troubleshooting-guide/debugging-tools/#oauth-2-0-tuto...

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