Accessing FitBit API via R (no web scraping) -
does have working r script access api? see there webscraping package, i'd prefer use official api if possible.
everything i've got , research i've done authenticate via oauth 1 or 2 seems broken using httr package.
for oauth 2:
library(httr) app = '<oauth 2.0 client id>' key <- '<client (consumer) key>' secret <- '<client (consumer) secret>' accesstokenurl <- 'https://api.fitbit.com/oauth2/token' authorizeurl <- 'https://www.fitbit.com/oauth2/authorize' fbr <- oauth_app(key,app,null) fitbit <- oauth_endpoint(null, authorizeurl,accesstokenurl) token <- oauth2.0_token(fitbit,fbr,scope='profile',as_header=true, cache=false)
when check token$credentials error message:
$errors $errors[[1]] $errors[[1]]$errortype [1] "oauth" $errors[[1]]$fieldname [1] "n/a" $errors[[1]]$message [1] "no authorization header provided in request. each call fitbit api should oauth signed" $success [1] false
i have tried misc settings in fitbit setting app, both server , client, , have callback url correctly set http://localhost:1410
any ideas?
thanks, isaac
ps: i've crossposted on fitbit's forums: https://twitter.com/isaacwyatt/status/637768649290350592 , tweeted @ hadley wickham help. retweets welcome!
current session info: r version 3.1.2 (2014-10-31) platform: x86_64-apple-darwin13.4.0 (64-bit) locale: [1] en_us.utf-8/en_us.utf-8/en_us.utf-8/c/en_us.utf-8/en_us.utf-8 attached base packages: [1] stats graphics grdevices utils datasets methods base other attached packages: [1] httr_1.0.0 loaded via namespace (and not attached): [1] curl_0.9.3 httpuv_1.3.2 jsonlite_0.9.14 r6_2.0.1 rcpp_0.11.4 stringr_0.6.2 [7] tools_3.1.2
fitbit's api access via oauth1.0a deprecated 2016-03-14, here working script using oauth2.0. there issue httr
package not being able request token using http basic authentication (required fitbit), fixed new use_basic_auth
argument oauth2.0_token()
.
fitbit application settings
- oauth 2.0 application type: personal
- callback url: http://localhost:1410 (this default expected
httr
)
example
# until version *following* httr 1.0.0 available on cran, # development version uncommenting following 2 lines # library(devtools) # install_github("hadley/httr") library(httr) # 1. set credentials fitbit_endpoint <- oauth_endpoint( request = "https://api.fitbit.com/oauth2/token", authorize = "https://www.fitbit.com/oauth2/authorize", access = "https://api.fitbit.com/oauth2/token") myapp <- oauth_app( appname = "data_access", key = "your oauth 2.0 client id", secret = "your client (consumer) secret") # 2. oauth token scope <- c("sleep","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/-/sleep/date/2015-10-10.json", config(token = fitbit_token)) content(resp)
Comments
Post a Comment