ROAuth R和FitBit API错误:未提供授权标头

时间:2012-07-20 22:45:39

标签: r oauth fitbit

使用R和Jeff Gentry的ROAuth软件包尝试从fitbit中提取数据,并且身份验证似乎不起作用。代码如下:

apiURL = 'api.fitbit.com/'

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

然后我运行握手:

> 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: 

完成授权并粘贴oauth_verifier标记,从而生成一组正确的凭据。

最后,我尝试获取我之后的个人资料数据:

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

我得到了回应:

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

2 个答案:

答案 0 :(得分:1)

好的,在用DTL和Geoff Jentry进行一些挖掘和发送电子邮件之后终于解决了这个问题(非常感谢你们)。

在最初的ROAuth包中,oauthGet函数没有使用Authorization .opt进行curl调用,并且还有类似下面的参数:

params <- c(params, as.list(auth))
getForm(url, .params = params, curl = curl, .opts = c(list(httpget = TRUE),  opts,  list(...))))

Fitbit.com Api有点特别https://wiki.fitbit.com/display/API/OAuth+Authentication+in+the+Fitbit+API需要“包装oauth_params的值,我做了以下mod:

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))

似乎指定参数并列出选项会导致问题。

最后获得了具有正确数据的表格!

答案 1 :(得分:0)

如果您还没有,请确保您拥有CRAN上没有的最新版本(0.9.2):

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

如果您使用的是Windows,则需要使用此功能:

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

还有其他人正在研究这个软件包的未来发展,我原本以为他们已经有了一个新版本,但显然没有,我应该提交0.9.2给CRAN,以防万一继续服用而

如果这不起作用,它可能是Fitbit特有的东西。我见过一些网站在ROAuth上打得不好。让我知道0.9.2是否仍然失败,我会试着看看它。