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

Problem in access token

Hi,
I'm trying to get data user data. After executing the below code the callback URL results in -

http://localhost:8080/?code=f2c2b46a44d8981f9c0d5e0ededa3af6e973ee9c#_=_

I'm not getting the access token and the user_id in the callback URL rather I'm getting code.
Where am I going wrong???

 

index.html

<!DOCTYPE html>
<html>
<head>
<title>Fitbit API JavaScript</title>
</head>
<body>
Login to Fitbit
</a>
<script src="app.js"></script>
</body>
</html>
  
app.js
// get the url
var url = window.location.href;
//getting the access token from url
var access_token = url.split("#")[1].split("=")[1].split("&")[0];
// get the userid
var userId = url.split("#")[1].split("=")[1].split("&")[0];
console.log(access_token);
console.log(userId);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.fitbit.com/1/user/' + userId + '/activities/heart/date/today/1w.json');
xhr.setRequestHeader("Authorization", 'Bearer ' + access_token);
xhr.onload = function () {
if (xhr.status === 200) {
console.log(xhr.responseText)
}
};
xhr.send()
Best Answer
0 Votes
1 REPLY 1

Hi @admissionraju25 

 

The value "f2c2b46a44d8981f9c0d5e0ededa3af6e973ee9c" is the authorization code.  You need to send Fitbit the authorization code to receive the access token and refresh token.  Here's an example

 

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

client_id=22942C&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fexample.com%2Ffitbit_auth&code=1234567890

 

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