From 65a6f2728c168e052ba429039bb3ea6c7f98e6ae Mon Sep 17 00:00:00 2001 From: Marcin Szymanski Date: Wed, 24 May 2017 21:46:50 +0000 Subject: [PATCH] improve handling of OAuth2 tokens --- RGoogleFit/DESCRIPTION | 2 +- RGoogleFit/NAMESPACE | 1 + RGoogleFit/NEWS | 5 +++++ RGoogleFit/R/GetFitOAuth2Token.R | 29 ++++++++++++++++++++++------ RGoogleFit/R/RGoogleFit-package.R | 2 ++ RGoogleFit/man/GetFitDataset.Rd | 1 - RGoogleFit/man/GetFitDatasource.Rd | 1 - RGoogleFit/man/GetFitDatasources.Rd | 1 - RGoogleFit/man/GetFitOauth2Object.Rd | 11 +++++++++++ RGoogleFit/man/GetFitOauth2Token.Rd | 7 ++++--- RGoogleFit/man/NanosToPOSIXct.Rd | 1 - RGoogleFit/man/RGoogleFit-package.Rd | 1 - 12 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 RGoogleFit/man/GetFitOauth2Object.Rd diff --git a/RGoogleFit/DESCRIPTION b/RGoogleFit/DESCRIPTION index 1a5396c..52ea449 100644 --- a/RGoogleFit/DESCRIPTION +++ b/RGoogleFit/DESCRIPTION @@ -15,4 +15,4 @@ Depends: httr Imports: utils -RoxygenNote: 5.0.1 +RoxygenNote: 6.0.1 diff --git a/RGoogleFit/NAMESPACE b/RGoogleFit/NAMESPACE index cd26925..3c04882 100644 --- a/RGoogleFit/NAMESPACE +++ b/RGoogleFit/NAMESPACE @@ -3,6 +3,7 @@ export(GetFitDataset) export(GetFitDatasource) export(GetFitDatasources) +export(GetFitOauth2Object) export(GetFitOauth2Token) export(NanosToPOSIXct) import(RCurl) diff --git a/RGoogleFit/NEWS b/RGoogleFit/NEWS index 81c0e07..6943f1c 100644 --- a/RGoogleFit/NEWS +++ b/RGoogleFit/NEWS @@ -1,3 +1,8 @@ +RGoogleFit 0.3.0 +---------------------------------------------------------------- + +* improved handling of OAuth2 tokens + RGoogleFit 0.2.0 ---------------------------------------------------------------- diff --git a/RGoogleFit/R/GetFitOAuth2Token.R b/RGoogleFit/R/GetFitOAuth2Token.R index 3ee9165..1a83383 100644 --- a/RGoogleFit/R/GetFitOAuth2Token.R +++ b/RGoogleFit/R/GetFitOAuth2Token.R @@ -2,8 +2,10 @@ #' @rdname GetFitOauth2Token #' @description #' Retrieves or refreshes an OAuth2 token. Two options must be set: -#' \code{RGoogleFit.client_id} -#' \code{RGoogleFit.client_secret} +#' \itemize{ +#' \item \code{RGoogleFit.client_id} +#' \item \code{RGoogleFit.client_secret} +#' } #' @export GetFitOauth2Token <- function() { client_id = getOption('RGoogleFit.client_id') @@ -14,7 +16,8 @@ GetFitOauth2Token <- function() { assert(!is.null(client_secret), 'Please set \'RGoogleFit.client_secret\' option') - oauth2_token <- get0('RGoogleFit.token') + oauth2_token <- RGoogleFitObjects$oauth2_object + print(oauth2_token) if (!is.null(oauth2_token)) { if (!oauth2_token$validate()) { @@ -38,10 +41,24 @@ GetFitOauth2Token <- function() { use_oob = FALSE, cache = TRUE ) + if (!oauth2_token$validate()) { + oauth2_token$refresh() + } } - assign('RGoogleFit.token', oauth2_token) - + RGoogleFitObjects$oauth2_object <- oauth2_token + return(oauth2_token$credentials$access_token) -} \ No newline at end of file +} + +#' @title GetFitOauth2Object +#' @rdname GetFitOauth2Object +#' @description +#' Returns the whole Oauth2 object. Useful for debugging purposes. +#' @export +GetFitOauth2Object <- function() { + + return(RGoogleFitObjects$oauth2_object) + +} diff --git a/RGoogleFit/R/RGoogleFit-package.R b/RGoogleFit/R/RGoogleFit-package.R index 5d90ee3..bf73750 100644 --- a/RGoogleFit/R/RGoogleFit-package.R +++ b/RGoogleFit/R/RGoogleFit-package.R @@ -11,3 +11,5 @@ #' @import RCurl jsonlite bit64 httr #' @importFrom utils URLencode invisible(NULL) + +RGoogleFitObjects <- new.env() \ No newline at end of file diff --git a/RGoogleFit/man/GetFitDataset.Rd b/RGoogleFit/man/GetFitDataset.Rd index 4ab35c7..0829add 100644 --- a/RGoogleFit/man/GetFitDataset.Rd +++ b/RGoogleFit/man/GetFitDataset.Rd @@ -20,4 +20,3 @@ Retrieves a dataset for a given \code{datasource} and time range defined by \code{startTime} and \code{endTime}. Refer to \url{https://developers.google.com/fit/rest/v1/datasets} for full documentation. } - diff --git a/RGoogleFit/man/GetFitDatasource.Rd b/RGoogleFit/man/GetFitDatasource.Rd index 4c72430..355f9cd 100644 --- a/RGoogleFit/man/GetFitDatasource.Rd +++ b/RGoogleFit/man/GetFitDatasource.Rd @@ -15,4 +15,3 @@ GetFitDatasource(token, datasource) Returns a list with datasource attributes. Refer to \url{https://developers.google.com/fit/rest/v1/data-types} for full documentation. } - diff --git a/RGoogleFit/man/GetFitDatasources.Rd b/RGoogleFit/man/GetFitDatasources.Rd index 13ee564..84d55b8 100644 --- a/RGoogleFit/man/GetFitDatasources.Rd +++ b/RGoogleFit/man/GetFitDatasources.Rd @@ -12,4 +12,3 @@ GetFitDatasources(token) \description{ \code{GetFitDatasources} returns a dataframe with user's datasources. } - diff --git a/RGoogleFit/man/GetFitOauth2Object.Rd b/RGoogleFit/man/GetFitOauth2Object.Rd new file mode 100644 index 0000000..1d8686b --- /dev/null +++ b/RGoogleFit/man/GetFitOauth2Object.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/GetFitOAuth2Token.R +\name{GetFitOauth2Object} +\alias{GetFitOauth2Object} +\title{GetFitOauth2Object} +\usage{ +GetFitOauth2Object() +} +\description{ +Returns the whole Oauth2 object. Useful for debugging purposes. +} diff --git a/RGoogleFit/man/GetFitOauth2Token.Rd b/RGoogleFit/man/GetFitOauth2Token.Rd index 00dae0c..88a80ca 100644 --- a/RGoogleFit/man/GetFitOauth2Token.Rd +++ b/RGoogleFit/man/GetFitOauth2Token.Rd @@ -8,7 +8,8 @@ GetFitOauth2Token() } \description{ Retrieves or refreshes an OAuth2 token. Two options must be set: -\code{RGoogleFit.client_id} -\code{RGoogleFit.client_secret} +\itemize{ +\item \code{RGoogleFit.client_id} +\item \code{RGoogleFit.client_secret} +} } - diff --git a/RGoogleFit/man/NanosToPOSIXct.Rd b/RGoogleFit/man/NanosToPOSIXct.Rd index 72ec6dd..6508033 100644 --- a/RGoogleFit/man/NanosToPOSIXct.Rd +++ b/RGoogleFit/man/NanosToPOSIXct.Rd @@ -15,4 +15,3 @@ Converts nanoseconds from epoch (as provided by Google Fit) to POSIXct \examples{ NanosToPOSIXct(1388534400000000000) } - diff --git a/RGoogleFit/man/RGoogleFit-package.Rd b/RGoogleFit/man/RGoogleFit-package.Rd index 864de64..24314b2 100644 --- a/RGoogleFit/man/RGoogleFit-package.Rd +++ b/RGoogleFit/man/RGoogleFit-package.Rd @@ -10,4 +10,3 @@ Provides interface to Google Fit REST API v1 Sources available at https://github.com/ms32035/RGoogleFit } -