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

R error 401 bad headers

Using the following R code to get my token:

 

library(httr)

appname = "HeartRate"
key = "2282KN"
secret = "31d.........................607"

request = "https://api.fitbit.com/oauth2/token"
authorize ="http://localhost:1410"
access = "https://api.fitbit.com/oauth2/token"
endpoint <- httr::oauth_endpoint(request, authorize, access)
myapp <- httr::oauth_app("r-package", key, secret)

scope <- c("sleep", "activity", "heartrate", "location", "nutrition", "profile", "settings", "social", "weight")

# 2. Get OAuth token
fitbit_token <- oauth2.0_token(endpoint, myapp, scope = scope, use_basic_auth = TRUE, user_params = endpoint)

# 3. Make API requests
resp <- GET(url="https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json")

content(resp)

I seem to get the token just fine:

Authentication complete. Please close this page and return to R.

 

I make the GET request but the response is an error 401

 

invalid_client"

$errors[[1]]$message
[1] "Invalid authorization header format.

 

The Response contains: 

R GET Response in RStudio

 

 I know its a problem with headers but I don't know how to change the code to make it work. Can anyone see whats wrong with the code.

 

ThanksSmiley Frustrated

Best Answer
0 Votes
4 REPLIES 4

@Eoghan Are you attempting to get Intraday data from someone other than the owner of the app? If you don't have access to Intraday Time Series, then only the owner of the app can pull their own Intraday data using a 'Personal' app.

Andrew | Community Moderator, Fitbit

What motivates you?

Best Answer
0 Votes

Andrew,

 

 I am trying to access my own app called HeartRate which is set to 'personal', using my own client Id and secret. 

 

 When I call:

fitbit_token <- oauth2.0_token(endpoint, myapp, scope = scope, use_basic_auth = TRUE, user_params = endpoint)

 

 my browser pops up saying authentication complete. Notice the URL.

 

http://localhost:1410/?client_id=2282KN&scope=sleep%20activity%20heartrate%20location%20nutrition%20profile%20settings%20social%20weight&redirect_uri=http%3A%2F%2Flocalhost%3A1410%2F&response_type=code&state=KQmkk0aylo

uri.jpg

but I dont get to the normal fitbit authorization form seen below:

fbauth.jpg

 

is this beacuse I use:

authorize ="http://localhost:1410"

instead of 

authorize = "http://www.fitbit.com/oauth/authorize"

which I see in many examples around the web, but doesn't seem to work either.

 

The only request I try is:

GET(url="https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json")

which is'nt Intraday is it?

 

Any help appreciated. Im so close to geting it working.

Best Answer
0 Votes

I changed my code to this

authorize = "http://www.fitbit.com/oauth/authorize"

now I get a used token message

 

Moderator edit: personal info removed

Best Answer
0 Votes

@Eoghan wrote:

I changed my code to this

authorize = "http://www.fitbit.com/oauth/authorize"

@Eoghan it should be:

authorize = "http://www.fitbit.com/oauth2/authorize"

 

You're missing the '2' after 'oauth'.

Andrew | Community Moderator, Fitbit

What motivates you?

Best Answer
0 Votes