Question

Using R and the ROAuth package by Jeff Gentry to try and pull data off of fitbit and the Authentication doesn't seem to work. Code as follows:

apiURL = 'api.fitbit.com/'

credentials = OAuthFactory$new(consumerKey=key, 
                           consumerSecret=secret, 
                           requestURL=tokenURL,
                           accessURL=accessTokenURL,
                           authURL=authorizeURL
                           )

and then I run the handshake:

> credentials$handshake()
To enable the connection, please direct your web browser to: 
http://www.fitbit.com/oauth/authorize?oauth_token=036afa88a832bfffc72af485e38c1572
When complete, record the PIN given to you and provide it here: 

Complete the authorization and paste in the oauth_verifier token, resulting in a proper looking set of credentials.

Finally I try to get the profile data that I'm after:

rawToChar(credentials$OAuthRequest(paste(apiURL,"1/user/userID/profile.json", sep="", collapse=''), "GET"))

And I get this in response:

[1] "{\"errors\":[{\"errorType\":\"oauth\",\"fieldName\":\"n/a\",\"message\":\"No 
Authorization header provided in the request. Each call to Fitbit API should be OAuth 
signed\"}]}"
Was it helpful?

Solution

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!

OTHER TIPS

If you haven't already, make sure you have the latest version (0.9.2) which is not available on CRAN:

http://geoffjentry.hexdump.org/ROAuth_0.9.2.tar.gz

If you're using Windows you'll need to use this one:

http://geoffjentry.hexdump.org/ROAuth_0.9.2.zip

There are other people working on the future development of the package, I had thought that they were going to have a new release already but apparently not, I should probably submit 0.9.2 to CRAN just in case it continues to take a while.

If that doesn't work, it might be something peculiar to Fitbit. I've seen a handful of sites not play well w/ ROAuth. Let me know if 0.9.2 still fails and I'll try to take a look at it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top