Skip to content

Commit

Permalink
improve handling of OAuth2 tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ms32035 committed May 24, 2017
1 parent ef8cc22 commit 65a6f27
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion RGoogleFit/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Depends:
httr
Imports:
utils
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
1 change: 1 addition & 0 deletions RGoogleFit/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(GetFitDataset)
export(GetFitDatasource)
export(GetFitDatasources)
export(GetFitOauth2Object)
export(GetFitOauth2Token)
export(NanosToPOSIXct)
import(RCurl)
Expand Down
5 changes: 5 additions & 0 deletions RGoogleFit/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
RGoogleFit 0.3.0
----------------------------------------------------------------

* improved handling of OAuth2 tokens

RGoogleFit 0.2.0
----------------------------------------------------------------

Expand Down
29 changes: 23 additions & 6 deletions RGoogleFit/R/GetFitOAuth2Token.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()) {
Expand All @@ -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)

}
}

#' @title GetFitOauth2Object
#' @rdname GetFitOauth2Object
#' @description
#' Returns the whole Oauth2 object. Useful for debugging purposes.
#' @export
GetFitOauth2Object <- function() {

return(RGoogleFitObjects$oauth2_object)

}
2 changes: 2 additions & 0 deletions RGoogleFit/R/RGoogleFit-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
#' @import RCurl jsonlite bit64 httr
#' @importFrom utils URLencode
invisible(NULL)

RGoogleFitObjects <- new.env()
1 change: 0 additions & 1 deletion RGoogleFit/man/GetFitDataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion RGoogleFit/man/GetFitDatasource.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion RGoogleFit/man/GetFitDatasources.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions RGoogleFit/man/GetFitOauth2Object.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions RGoogleFit/man/GetFitOauth2Token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion RGoogleFit/man/NanosToPOSIXct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion RGoogleFit/man/RGoogleFit-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65a6f27

Please sign in to comment.