Oauth Authentification to Fitbit Using Httr

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



Leave a reply



Submit