Skip to content

Commit

Permalink
add GetFitOauth2Token function
Browse files Browse the repository at this point in the history
  • Loading branch information
ms32035 committed Sep 20, 2016
1 parent dac79e4 commit ef8cc22
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
RGoogleFit.Rproj
RGoogleFit.Rcheck
*.tar.gz
.httr-oauth
1 change: 1 addition & 0 deletions RGoogleFit/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
.httr-oauth
5 changes: 3 additions & 2 deletions RGoogleFit/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
Description: Provides interface to Google Fit REST API v1 (see <https://developers.google.com/fit/rest/v1/reference/>).
Expand All @@ -11,7 +11,8 @@ Depends:
R (>= 3.0),
RCurl,
jsonlite,
bit64
bit64,
httr
Imports:
utils
RoxygenNote: 5.0.1
2 changes: 2 additions & 0 deletions RGoogleFit/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions RGoogleFit/NEWS
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RGoogleFit 0.2.0
----------------------------------------------------------------

* new function GetFitOauth2Token to simplify OAuth2 authentication

RGoogleFit 0.1.0
----------------------------------------------------------------

* initial release
47 changes: 47 additions & 0 deletions RGoogleFit/R/GetFitOAuth2Token.R
Original file line number Diff line number Diff line change
@@ -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)

}
2 changes: 1 addition & 1 deletion RGoogleFit/R/RGoogleFit-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#'
#' @name RGoogleFit-package
#' @docType package
#' @import RCurl jsonlite bit64
#' @import RCurl jsonlite bit64 httr
#' @importFrom utils URLencode
invisible(NULL)
5 changes: 4 additions & 1 deletion RGoogleFit/R/Utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ NanosToPOSIXct <- function(nanos) {
)

}


assert <- function (expr, error) {
if (! expr) stop(error, call. = FALSE)
}
14 changes: 14 additions & 0 deletions RGoogleFit/man/GetFitOauth2Token.Rd

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

0 comments on commit ef8cc22

Please sign in to comment.