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

init_oauth2 end point fail in R using httr

Howdy,

 

I'm trying to get access to my time series data on my Charge HR. The R-script below seems to be working in that the authentication works. Call back URI and authentication URI in the app are set to http://localhost:1410/.

 

However, after I authenticate I get this error:

 

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, :
Bad Request (HTTP 400).

 

Any ideas what could be causing this?

 

library(httr)

# 1. Set up credentials
fitbit_endpoint <- oauth_endpoint(
  request = "https://api.fitbit.com/oauth2/token",
  authorize = "http://localhost:1410/",
  access = "https://api.fitbit.com/oauth2/token")
myapp <- oauth_app(
  appname = "data_access",
  key = "MYUSER", 
  secret = "MYSECRET")

# 2. Get OAuth 
# See dev.fitbit.com/docs/oauth2/#scope
scope <- c("activities")
           #,"activity", "heartrate", "nutrition", "weight")  
fitbit_token <- oauth2.0_token(fitbit_endpoint, myapp,
                               scope = scope, use_basic_auth = TRUE)

# 3. Make API requests
resp <- GET(url = "GET /1/user/[username]/activities/tracker/date/2016-01-01/today.json", 
            config(token = fitbit_token))
content(resp)

 

Best Answer
0 Votes
9 REPLIES 9

Any one?

Best Answer
0 Votes

I got a little bit further on my path by passing some endpoint_parameters to the oauth2.0token

 

fitbit_endpoint <- oauth_endpoint(
  request = "https://api.fitbit.com/oauth2/token",
  authorize = host, #http://localhost:1410/
  access = "https://api.fitbit.com/oauth2/token")
myapp <- oauth_app(
  appname = "data_access",
  key = client, 
  secret = token)

# 2. Get OAuth token
scope <- c("activity")  # See dev.fitbit.com/docs/oauth2/#scope
fitbit_token <- oauth2.0_token(fitbit_endpoint, myapp,
                               scope = scope, use_basic_auth = TRUE,
                               user_params = fitbit_endpoint)

# 3. Make API requests
resp <- GET(url = "https://api.fitbit.com/1/user/-/activities/date/2016-01-01/2016-04-09.json", 
            config(token = fitbit_token))
content(resp)

Unfortunately, when I do content on 'resp' I get this error:

 

$errors
$errors[[1]]
$errors[[1]]$errorType
[1] "invalid_client"

$errors[[1]]$message
[1] "Invalid authorization header format. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."

Best Answer
0 Votes

I've also got to this point - did you get any further?

Best Answer
0 Votes

I'm stuck in this part. 

library(httr)
library(xml2)
library(jsonlite)
fitbit_endpoint <- oauth_endpoint(
  request = "https://api.fitbit.com/oauth2/token",
  authorize = "http://localhost:1410",
  access = "https://api.fitbit.com/oauth2/token")

myapp <- oauth_app(
  appname = "app",
  key = "key", #my consumer key
  secret = "secret")

# 2. Get OAuth token
scope <- c("activity")  # See dev.fitbit.com/docs/oauth2/#scope
fitbit_token <- oauth2.0_token(fitbit_endpoint, myapp,
                               scope = scope, use_basic_auth = TRUE)
# 3. Make API requests
resp <- GET(url = "https://api.fitbit.com/1/user/-/activities/steps/date/today/1m.json", 
            config(token = fitbit_token))
content(resp)

And I get this: 

 

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, :
Bad Request (HTTP 400).
In addition: Warning message:
In strsplit(rawToChar(raw), "\r?\n") :
input string 1 is invalid in this locale

Best Answer
0 Votes

For those that find the same forum postings when looking for a solution to import fitbit data into R, the R-package "fitbitr" (https://github.com/teramonagi/fitbitr) works for me.

 

One additional hint; when defining your application in the fitbit api, set the oauth2 application type to 'server'.

Best Answer
0 Votes

I've tried the teramonagi/fitbitr as well as the Avsecz/fitbitr. Both times I'm getting and error. Can't figure out what's up.

Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params, :
Unauthorized (HTTP 401). Failed to get an access token.

Best Answer
0 Votes

Same here.. used to work beginning of this year, maybe there has been some update somewhere?

 

Not really helpful, but would love a post here with a solution 🙂

Best Answer
0 Votes

@zjuul please try to use this page: https://dev.fitbit.com/apps/oauthinteractivetutorial

See if you will be able to get access token and make requests using curl.

 

Best Answer
0 Votes

That worked.. but not in my script.. 🙂

 

Dug a bit deeper, and via this URL - https://github.com/r-lib/httr/issues/482 found that upgrading my libs might get it working.

After doing that AND after creating a new fitbit application (new app name + keys) I got it working.

Thanks all.

Best Answer
0 Votes