Oauth authentification to Fitbit using httr
The problem comes from the httr library, that uses curlEscape for encoding paramaters while the OAuth 1.0 specifications requires percent encoding (see this page).
Replacing calls to curlEscape with curlPercentEncode solves the issue!
many thanks to @mark-s for his help.
Accessing FitBit API via R (no web scraping)
So I asked for help on fitbit's forums as well, and user grahamrp posted the following using oauth 1.0:
library(httr)
token_url = 'https://api.fitbit.com/oauth/request_token'
access_url = 'https://api.fitbit.com/oauth/access_token'
auth_url = 'https://www.fitbit.com/oauth/authorize'
key <- '<Client (Consumer) Key>'
secret <- '<Client (Consumer) Secret>'
myapp = oauth_app('data_access', key, secret)
fb_ep = oauth_endpoint(token_url, auth_url, access_url)
token = oauth1.0_token(fb_ep, myapp)
gtoken = config(token = token)
resp = GET('https://api.fitbit.com/1/user/-/sleep/date/today.json', gtoken)
content(resp)
This works for me as of 8/31/2015
Link: https://community.fitbit.com/t5/Web-API/Working-R-script-Using-API-R-Mac-OS-X/m-p/931586/highlight/false#M3002
Thanks all for your attention to this!
Best,
Isaac
OAuth2 Implicit vs Authorization Code Grant for Fitbit API
In the above example, you appear to have missing a separator between the "Basic" header and it's value in this line:
request.setRequestHeader('Authorization', 'Basic'+ encoded_string);
Provided you are building a web based applications, you may want to look into using the 'Implicit' Flow, too: https://oauth.net/2/grant-types/implicit/
ROAuth R and FitBit API Error: No Authorization header provided
Okay finally solved the issue after some digging and emailing with DTL and Geoff Jentry (thanks so much guys).
In the original ROAuth package the oauthGet function did not use the Authorization .opt for the curl call and also had params that looked like this:
params <- c(params, as.list(auth))
getForm(url, .params = params, curl = curl, .opts = c(list(httpget = TRUE), opts, list(...))))
Fitbit.com Api was a little more particular https://wiki.fitbit.com/display/API/OAuth+Authentication+in+the+Fitbit+API needing " to wrap the values of the oauth_params and I made the following mods:
params <-as.list(auth) #dropping the first item in the list which was an extra "GET"
opts=list(httpheader=c(Authorization=paste("OAuth ", paste(names(auth), '="', auth, '"', sep = "", collapse = ",\n "), sep="", collapse='')))
getForm(url, curl = curl, .opts = c( opts))
It seems that specifying the params and listing the options was causing issues.
Finally the form with correct data was obtained!
Related Topics
How to Rotate the X-Axis Labels 90 Degrees in Levelplot
Plotting Multiple Lines from a Data Frame with Ggplot2
Easiest Way to Discretize Continuous Scales for Ggplot2 Color Scales
Grid.Arrange Using List of Plots
Percentage Histogram with Facet_Wrap
Ggplot2:How to Reduce the Width and the Space Between Bars with Geom_Bar
How Does One Merge Dataframes by Row Name Without Adding a "Row.Names" Column
Changing Word Template for Knitr in Rmarkdown
Passing a 'Data.Table' to C++ Functions Using 'Rcpp' And/Or 'Rcpparmadillo'
Can Lapply Not Modify Variables in a Higher Scope
Cumulative Count of Unique Values in R
How to Replace Numeric Codes with Value Labels from a Lookup Table
Removing a List of Columns from a Data.Frame Using Subset
How to Ddply() Without Sorting
How to Create Geom_Boxplot with Large Amount of Continuous X-Variables