通过 R 访问 FitBit API(无网页抓取)

Accessing FitBit API via R (no web scraping)

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

为了通过 oauth 1 或 2 进行身份验证,我所拥有的一切以及我所做的研究似乎都无法使用 httr 包。

对于 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    

所以我也在 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 日,这对我有效 Link: https://community.fitbit.com/t5/Web-API/Working-R-script-Using-API-R-Mac-OS-X/m-p/931586/highlight/false#M3002

感谢大家的关注!

最佳,

以撒

Fitbit 通过 OAuth1.0a 的 API 访问 deprecated from 2016-03-14, so here is working script using OAuth2.0. There was an issue httr 包无法使用 HTTP 基本身份验证(Fitbit 要求)请求令牌,但这是由 oauth2.0_token().

的新 use_basic_auth 参数修复

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)