Last active
November 16, 2015 06:25
-
-
Save cwickham/81be4a3c2f6eb8caa94d to your computer and use it in GitHub Desktop.
httr authentication functions for fitbit API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # edited from init_oauth2.0 to: | |
| # * add trailing slash to callback | |
| # * add base64 encoded client_id:client_secret to request when exchanging | |
| init_oauth2.0_fitbit <- function(endpoint, app, scope = NULL, type = NULL, | |
| use_oob = getOption("httr_oob_default"), | |
| is_interactive = interactive()) { | |
| if (!use_oob && !httr:::is_installed("httpuv")) { | |
| message("httpuv not installed, defaulting to out-of-band authentication") | |
| use_oob <- TRUE | |
| } | |
| if (isTRUE(use_oob)) { | |
| stopifnot(interactive()) | |
| redirect_uri <- "urn:ietf:wg:oauth:2.0:oob" | |
| state <- NULL | |
| } else { | |
| redirect_uri <- paste0(oauth_callback(), "/") | |
| state <- httr:::nonce() | |
| } | |
| scope_arg <- paste(scope, collapse = ' ') | |
| authorize_url <- modify_url(endpoint$authorize, | |
| query = httr:::compact(list( | |
| client_id = app$key, | |
| scope = scope_arg, | |
| redirect_uri = redirect_uri, | |
| response_type = "code", | |
| state = state)) | |
| ) | |
| if (isTRUE(use_oob)) { | |
| code <- oauth_exchanger(authorize_url)$code | |
| } else { | |
| code <- oauth_listener(authorize_url, is_interactive)$code | |
| } | |
| auth <- paste0("Basic ", | |
| base64enc::base64encode(charToRaw(paste0(app$key, ":", app$secret)))) | |
| # Use authorisation code to get (temporary) access token | |
| req <- POST(endpoint$access, encode = "form", | |
| add_headers("Authorization" = auth), | |
| body = list( | |
| client_id = app$key, | |
| redirect_uri = redirect_uri, | |
| grant_type = "authorization_code", | |
| code = code)) | |
| content(req, type = type) | |
| } | |
| # edited from refresh_oauth2.0 to: | |
| # * add base64 encoded client_id:client_secret to request when exchanging | |
| refresh_oauth2.0_fitbit <- function(endpoint, app, credentials) { | |
| if (is.null(credentials$refresh_token)) { | |
| stop("Refresh token not available", call. = FALSE) | |
| } | |
| refresh_url <- endpoint$access | |
| # Use authorisation code to get (temporary) access token | |
| body <- list( | |
| refresh_token = credentials$refresh_token, | |
| client_id = app$key, | |
| client_secret = app$secret, | |
| grant_type = "refresh_token" | |
| ) | |
| auth <- paste0("Basic ", | |
| base64enc::base64encode(charToRaw(paste0(app$key, ":", app$secret)))) | |
| response <- POST(refresh_url, body = body, encode = "form", | |
| add_headers("Authorization" = auth)) | |
| stop_for_status(response) | |
| refresh_data <- content(response) | |
| modifyList(credentials, refresh_data) | |
| } | |
| oauth2.0_token_fitbit <- function(endpoint, app, scope = NULL, type = NULL, | |
| use_oob = getOption("httr_oob_default"), | |
| as_header = TRUE, | |
| cache = getOption("httr_oauth_cache")) { | |
| params <- list(scope = scope, type = type, use_oob = use_oob, | |
| as_header = as_header) | |
| Token2.0_fitbit$new(app = app, endpoint = endpoint, params = params, | |
| cache_path = cache) | |
| } | |
| Token2.0_fitbit <- R6::R6Class("Token2.0_fitbit", inherit = Token2.0, list( | |
| init_credentials = function() { | |
| self$credentials <- init_oauth2.0_fitbit(self$endpoint, self$app, | |
| scope = self$params$scope, type = self$params$type, | |
| use_oob = self$params$use_oob) | |
| }, | |
| refresh = function() { | |
| self$credentials <- refresh_oauth2.0_fitbit(self$endpoint, self$app, self$credentials) | |
| self$cache() | |
| self | |
| } | |
| )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment