From ef8cc222d2bc38aa47bba8404eb5f60cb5c16d43 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 20 Sep 2016 21:58:09 +0000 Subject: [PATCH] add GetFitOauth2Token function --- .gitignore | 1 + RGoogleFit/.Rbuildignore | 1 + RGoogleFit/DESCRIPTION | 5 +-- RGoogleFit/NAMESPACE | 2 ++ RGoogleFit/NEWS | 9 ++++++ RGoogleFit/R/GetFitOAuth2Token.R | 47 +++++++++++++++++++++++++++++ RGoogleFit/R/RGoogleFit-package.R | 2 +- RGoogleFit/R/Utils.R | 5 ++- RGoogleFit/man/GetFitOauth2Token.Rd | 14 +++++++++ 9 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 RGoogleFit/NEWS create mode 100644 RGoogleFit/R/GetFitOAuth2Token.R create mode 100644 RGoogleFit/man/GetFitOauth2Token.Rd diff --git a/.gitignore b/.gitignore index 3de7b17..3e45267 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ RGoogleFit.Rproj RGoogleFit.Rcheck *.tar.gz +.httr-oauth \ No newline at end of file diff --git a/RGoogleFit/.Rbuildignore b/RGoogleFit/.Rbuildignore index 91114bf..900464a 100644 --- a/RGoogleFit/.Rbuildignore +++ b/RGoogleFit/.Rbuildignore @@ -1,2 +1,3 @@ ^.*\.Rproj$ ^\.Rproj\.user$ +.httr-oauth \ No newline at end of file diff --git a/RGoogleFit/DESCRIPTION b/RGoogleFit/DESCRIPTION index 3c414c0..1a5396c 100644 --- a/RGoogleFit/DESCRIPTION +++ b/RGoogleFit/DESCRIPTION @@ -1,7 +1,7 @@ Package: RGoogleFit Type: Package Title: R Interface to Google Fit API -Version: 0.1.0 +Version: 0.2.0 Author: Marcin Szymanski Maintainer: Marcin Szymanski Description: Provides interface to Google Fit REST API v1 (see ). @@ -11,7 +11,8 @@ Depends: R (>= 3.0), RCurl, jsonlite, - bit64 + bit64, + httr Imports: utils RoxygenNote: 5.0.1 diff --git a/RGoogleFit/NAMESPACE b/RGoogleFit/NAMESPACE index 30806ab..cd26925 100644 --- a/RGoogleFit/NAMESPACE +++ b/RGoogleFit/NAMESPACE @@ -3,8 +3,10 @@ export(GetFitDataset) export(GetFitDatasource) export(GetFitDatasources) +export(GetFitOauth2Token) export(NanosToPOSIXct) import(RCurl) import(bit64) +import(httr) import(jsonlite) importFrom(utils,URLencode) diff --git a/RGoogleFit/NEWS b/RGoogleFit/NEWS new file mode 100644 index 0000000..81c0e07 --- /dev/null +++ b/RGoogleFit/NEWS @@ -0,0 +1,9 @@ +RGoogleFit 0.2.0 +---------------------------------------------------------------- + +* new function GetFitOauth2Token to simplify OAuth2 authentication + +RGoogleFit 0.1.0 +---------------------------------------------------------------- + +* initial release \ No newline at end of file diff --git a/RGoogleFit/R/GetFitOAuth2Token.R b/RGoogleFit/R/GetFitOAuth2Token.R new file mode 100644 index 0000000..3ee9165 --- /dev/null +++ b/RGoogleFit/R/GetFitOAuth2Token.R @@ -0,0 +1,47 @@ +#' @title GetFitOauth2Token +#' @rdname GetFitOauth2Token +#' @description +#' Retrieves or refreshes an OAuth2 token. Two options must be set: +#' \code{RGoogleFit.client_id} +#' \code{RGoogleFit.client_secret} +#' @export +GetFitOauth2Token <- function() { + client_id = getOption('RGoogleFit.client_id') + client_secret = getOption('RGoogleFit.client_secret') + + assert(!is.null(client_id), + 'Please set \'RGoogleFit.client_id\' option') + assert(!is.null(client_secret), + 'Please set \'RGoogleFit.client_secret\' option') + + oauth2_token <- get0('RGoogleFit.token') + + if (!is.null(oauth2_token)) { + if (!oauth2_token$validate()) { + oauth2_token$refresh() + } + + } + else + { + oauth2_token <- oauth2.0_token( + endpoint = oauth_endpoints("google"), + app = oauth_app( + "google", + key = getOption("RGoogleFit.client_id"), + secret = getOption("RGoogleFit.client_secret") + ), + scope = c( + "https://www.googleapis.com/auth/fitness.activity.read", + "https://www.googleapis.com/auth/fitness.location.read" + ) , + use_oob = FALSE, + cache = TRUE + ) + } + + assign('RGoogleFit.token', oauth2_token) + + return(oauth2_token$credentials$access_token) + +} \ No newline at end of file diff --git a/RGoogleFit/R/RGoogleFit-package.R b/RGoogleFit/R/RGoogleFit-package.R index de0cc78..5d90ee3 100644 --- a/RGoogleFit/R/RGoogleFit-package.R +++ b/RGoogleFit/R/RGoogleFit-package.R @@ -8,6 +8,6 @@ #' #' @name RGoogleFit-package #' @docType package -#' @import RCurl jsonlite bit64 +#' @import RCurl jsonlite bit64 httr #' @importFrom utils URLencode invisible(NULL) diff --git a/RGoogleFit/R/Utils.R b/RGoogleFit/R/Utils.R index 58880d3..2593262 100644 --- a/RGoogleFit/R/Utils.R +++ b/RGoogleFit/R/Utils.R @@ -43,4 +43,7 @@ NanosToPOSIXct <- function(nanos) { ) } - \ No newline at end of file + +assert <- function (expr, error) { + if (! expr) stop(error, call. = FALSE) +} \ No newline at end of file diff --git a/RGoogleFit/man/GetFitOauth2Token.Rd b/RGoogleFit/man/GetFitOauth2Token.Rd new file mode 100644 index 0000000..00dae0c --- /dev/null +++ b/RGoogleFit/man/GetFitOauth2Token.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/GetFitOAuth2Token.R +\name{GetFitOauth2Token} +\alias{GetFitOauth2Token} +\title{GetFitOauth2Token} +\usage{ +GetFitOauth2Token() +} +\description{ +Retrieves or refreshes an OAuth2 token. Two options must be set: +\code{RGoogleFit.client_id} +\code{RGoogleFit.client_secret} +} +