1

有没有人有一个有效的 R 脚本来访问 API?我看到有一个 webscraping 包,但如果可能的话,我更喜欢使用官方 API。

使用 httr 包,我通过 oauth 1 或 2 进行身份验证所获得的一切和研究似乎都被破坏了。

对于 oauth 2:

library(httr)
app = '<OAuth 2.0 Client ID>'
key <- '<Client (Consumer) Key>'
secret <- '<Client (Consumer) Secret>'
accessTokenURL <- 'https://api.fitbit.com/oauth2/token'
authorizeURL <- 'https://www.fitbit.com/oauth2/authorize'

fbr <- oauth_app(key,app,NULL)
fitbit <- oauth_endpoint(NULL, authorizeURL,accessTokenURL)
token <- oauth2.0_token(fitbit,fbr,scope='profile',as_header=TRUE, cache=FALSE)

当我检查 token$credentials 时,我收到错误消息:

$errors
$errors[[1]]
$errors[[1]]$errorType
[1] "oauth"

$errors[[1]]$fieldName
[1] "n/a"

$errors[[1]]$message
[1] "No Authorization header provided in the request. Each call to Fitbit API should be OAuth signed"



$success
[1] FALSE

我已经尝试了 fitbit 设置我的应用程序中的所有其他设置,作为服务器和客户端,我的回调 URL 正确设置为http://localhost:1410

有任何想法吗?

谢谢,艾萨克

ps:我在 fitbit 的论坛上发帖:https ://twitter.com/IsaacWyatt/status/637768649290350592并在 Hadley Wickham 发推文寻求帮助。欢迎转发!

Current session info:
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] httr_1.0.0

loaded via a namespace (and not attached):
[1] curl_0.9.3      httpuv_1.3.2    jsonlite_0.9.14 R6_2.0.1        Rcpp_0.11.4     stringr_0.6.2  
[7] tools_3.1.2    
4

2 回答 2

2

Fitbit 通过 OAuth1.0a 的 API 访问自 2016-03-14 起已弃用,因此这里是使用 OAuth2.0 的工作脚本。包无法使用 HTTP 基本身份验证(Fitbit 要求)请求令牌存在问题但这已通过.httruse_basic_authoauth2.0_token()

Fitbit 应用程序设置

  • OAuth 2.0 应用程序类型:个人
  • 回调 URL:http://localhost:1410(这是 预期的默认值httr

例子

# Until the version *following* httr 1.0.0 is available on CRAN, get the
# development version by uncommenting the following 2 lines
# library(devtools)
# install_github("hadley/httr")
library(httr)

# 1. Set up credentials
fitbit_endpoint <- oauth_endpoint(
  request = "https://api.fitbit.com/oauth2/token",
  authorize = "https://www.fitbit.com/oauth2/authorize",
  access = "https://api.fitbit.com/oauth2/token")
myapp <- oauth_app(
  appname = "data_access",
  key = "Your OAuth 2.0 Client ID", 
  secret = "Your Client (Consumer) Secret")

# 2. Get OAuth token
scope <- c("sleep","activity")  # See dev.fitbit.com/docs/oauth2/#scope
fitbit_token <- oauth2.0_token(fitbit_endpoint, myapp,
  scope = scope, use_basic_auth = TRUE)

# 3. Make API requests
resp <- GET(url = "https://api.fitbit.com/1/user/-/sleep/date/2015-10-10.json", 
  config(token = fitbit_token))
content(resp)
于 2016-01-09T19:33:12.913 回答
0

所以我也在 fitbit 的论坛上寻求帮助,用户 grahamrp 使用 oauth 1.0 发布了以下内容:

library(httr)
token_url = 'https://api.fitbit.com/oauth/request_token'
access_url = 'https://api.fitbit.com/oauth/access_token'
auth_url = 'https://www.fitbit.com/oauth/authorize'
key <- '<Client (Consumer) Key>'
secret <- '<Client (Consumer) Secret>'

myapp = oauth_app('data_access', key, secret)
fb_ep = oauth_endpoint(token_url, auth_url, access_url)
token = oauth1.0_token(fb_ep, myapp)
gtoken = config(token = token)

resp = GET('https://api.fitbit.com/1/user/-/sleep/date/today.json', gtoken)
content(resp)

截至 2015 年 8 月 31 日,这对我有用 链接:https ://community.fitbit.com/t5/Web-API/Working-R-script-Using-API-R-Mac-OS-X/mp/931586 /highlight/false#M3002

感谢大家对此的关注!

最好的,

艾萨克

于 2015-09-01T02:26:56.363 回答