Skip to content

Instantly share code, notes, and snippets.

@ccagrawal
Last active December 22, 2015 03:09
Show Gist options
  • Save ccagrawal/6408431 to your computer and use it in GitHub Desktop.
Save ccagrawal/6408431 to your computer and use it in GitHub Desktop.
Download FitBit step counts using API.
library(httr)
library(rjson)
key <- '<edited>'
secret <- '<edited>'
tokenURL <- 'http://api.fitbit.com/oauth/request_token'
accessTokenURL <- 'http://api.fitbit.com/oauth/access_token'
authorizeURL <- 'http://www.fitbit.com/oauth/authorize'
fbr <- oauth_app('MyHome', 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
)
data <- data.frame(seq(from = as.Date("2013-05-24"), to = as.Date("2013-08-31"), by = 1))
data$steps <- 0
colnames(data) <- c('date', 'steps')
for (index in 1:nrow(data)) {
URL <- paste('http://api.fitbit.com/1/user/-/activities/date/', data[index, 1], '.json', sep = '')
rawData <- GET(URL, sig)[[6]]
charData <- rawToChar(rawData, multiple = FALSE)
parseData <- fromJSON(charData, method = 'C')
data[index, 2] <- parseData$summary$steps
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment