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

OAUTH2 Invalid Authorization Token Type

Hi All I wrote a Fitbit App before and I am trying my luck with an application. 

 

I can get the request token, and I can use it to get the access token. I cannot use the access token to get the actual data. Below is just some rough work and I will be properly storing the tokens later, just trying to get the foundation working.

 

router.get('/fitbit-api-token',function(req,res) {
   const requestToken = req.query.code
   const headers = {
    'Authorization': `Basic REDACTED`,
    'Content-Type': 'application/x-www-form-urlencoded',
};
  const body = "cliend_id=REDACTED&grant_type=authorization_code&redirect_uri=http://localhost:3000/fitbit-api-token&code="+requestToken;
request({
    url: 'https://api.fitbit.com/oauth2/token',
    method: 'POST',
    headers,
    body,
},function (error, response, body){
    const fitbitResponse = JSON.parse(body)

    access_token = fitbitResponse.access_token;
    refresh_token = fitbitResponse.refresh_token;
    user_id = fitbitResponse.user_id;

    var json_headers = {
    'Authorization': 'Bearer ' + access_token,
   };
   console.log(json_headers);
   request({
       url: "https://api.fitbit.com/1/user/"+ user_id + "/activities/heart/date/today/1d.json",
       method: 'GET',
       json_headers,
     },function (error, response, body){
       console.log(body);
     });
});

I get an authorization error: Invalid Authorization token type when I am doing the GET method

Best Answer
0 Votes
4 REPLIES 4

Hi @vanmepper 

 

I'm not familiar with the programming language you're using.   I'm not sure if this has anything to do with your error, but I do see the word client_id misspelled in your code for the variable "body".  The misspelling could cause the  /oauth2/token call to fail would which not provide you with an access token.   Are you sure you're getting the access token, refresh token and user id from the endpoint?

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

@Gordon-C I am using Javascript.

 

I was still getting tokens which is weird. It doesn't return any errors and it appears to be correct. I did fix client_id and that doesnt change anything.

Below is the response I get with some details edited out.

 

vanmepper_1-1599606458813.png

 

 

Best Answer
0 Votes

@GordonFitbit I just tried using Postman and I get the expected response so it seems to be on my request side and not a token error. I will have to look into it more. 

Best Answer
0 Votes

I used a different function called fetch and used the mode set to 'cors' and that seemed to fix it. 

Best Answer
0 Votes