我正在尝试使用 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 <- 'api.fitbit/oauth/request_token' accessTokenURL <- 'api.fitbit/oauth/access_token' authorizeURL <- 'www.fitbit/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获得身份验证已完成.消息,但尝试访问api则会引发错误消息
I get the Authentication complete. message from httr, but trying to access the api then throws an error message
GET("api.fitbit/1/user/-/activities/date/2012-08-29.json", sig) Response [api.fitbit/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身份验证
发布评论