【问题标题】:ROAuth R and FitBit API Error: No Authorization header providedROAuth R 和 FitBit API 错误:未提供授权标头
【发布时间】:2012-07-20 05:36:41
【问题描述】:

使用 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\"}]}"

【问题讨论】:

    标签: r oauth fitbit


    【解决方案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 的值,我制作了以下模块:

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

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

    终于得到了正确数据的表格!

    【讨论】:

      【解决方案2】:

      如果您还没有,请确保您拥有 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 仍然失败,请告诉我,我会尝试看看它。

      【讨论】:

      • 仍然无法让它正确发送授权标头(我认为这是问题所在)。在 oauthGET 函数的某个地方,必须设置一些额外的 .opts,但我不知道是哪一个。写信给 RCurl 作者 Duncan Lang,看看他是否知道。
      • DTL 实际上是接管 ROAuth 的人之一,所以你可能会问他关于版本 0.99.2(他当前的开发版本)作为另一种可能性。
      • 好的,取得一些进展,对于 fitbit,看起来您需要在将 Oauth 参数作为值传递之前将它们用引号括起来。现在打开 RCurl 的 verbose=T 选项并获得更多有用的信息
      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2021-12-09
      • 2011-06-23
      相关资源
      最近更新 更多