使用 httr 对 Fitbit 进行 Oauth 身份验证 [英] Oauth authentification to Fitbit using httr

查看:35
本文介绍了使用 httr 对 Fitbit 进行 Oauth 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 httr 库连接到 fitbit api.

I'm trying to connect to the fitbit api using the httr library.

使用提供的示例,我想出了以下代码:

Using the examples provided, I came up with the following code:

library(httr)

key <- '<edited>'
secret <- '<edited>'
tokenURL <- 'http://api.fitbit.com/oauth/request_token'
accessTokenURL <- 'http://api.fitbit.com/oauth/access_token'
authorizeURL <- 'https://www.fitbit.com/oauth/authorize'

fbr <- oauth_app('fitbitR',key,secret)
fitbit <- oauth_endpoint(tokenURL,authorizeURL,accessTokenURL)

token <- oauth1.0_token(fitbit,fbr)
sig <- sign_oauth1.0(fbr,
    token=token$oauth_token,
    token_secret=token$oauth_token_secret
)

我收到了来自 httr 的 Authentication complete. 消息,但尝试访问 api 然后抛出错误消息

I get the Authentication complete. message from httr, but trying to access the api then throws an error message

GET("http://api.fitbit.com/1/user/-/activities/date/2012-08-29.json", sig)
Response [http://api.fitbit.com/1/user/-/activities/date/2012-08-29.json]
  Status: 401
  Content-type: application/x-www-form-urlencoded;charset=UTF-8
{"errors":[{"errorType":"oauth","fieldName":"oauth_access_token","message":"Invalid signature or token '<edited>' or token '<edited>'"}]} 

关于问题可能是什么的任何线索?

Any clue about what the problem might be?

推荐答案

问题来自 httr 库,它使用 curlEscape 对参数进行编码,而 OAuth 1.0 规范需要百分比编码(请参阅 此页面).

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

用 curlPercentEncode 替换对 curlEscape 的调用解决了这个问题!

Replacing calls to curlEscape with curlPercentEncode solves the issue!

非常感谢@mark-s 的帮助.

many thanks to @mark-s for his help.

这篇关于使用 httr 对 Fitbit 进行 Oauth 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆