-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function for google authentication automation
- Loading branch information
Showing
6 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
/data-RB | ||
/divewatchrshiny | ||
.secrets | ||
.config |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#' Uses the edit rights to a Google sheets if they are stored by the user | ||
#' | ||
#' Stores the authentication token in a file and uses it to access the Google sheet | ||
#' | ||
#' This function allows Github Action and a users that has a token stored to access a Google sheet to | ||
#' retrieve the data from a given google sheet | ||
#' | ||
#' @param email email account that will be used to access the Logbook in Google Spreadsheet | ||
#' | ||
#' @author Ruben Perez Perez | ||
#' | ||
#' @import gargle | ||
#' @import googlesheets4 | ||
#' @import base64enc | ||
#' | ||
#' @return It provides access to a given Google sheet if there is a token stored. | ||
#' | ||
#' @export | ||
#' @examples | ||
#' divewatch() | ||
#' \dontrun{ | ||
#' google_auth() | ||
#' } | ||
|
||
google_auth <- function(email = NA) { | ||
|
||
|
||
# Path to save the decoded OAuth token | ||
oauth_token_path <- ".config/gcloud/google-oauth-token.rds" | ||
|
||
# Check if we are running in GitHub Actions by looking for the environment variable | ||
if (Sys.getenv("GITHUB_ACTIONS") == "true") { | ||
# Retrieve the Base64-encoded OAuth token from the environment (GitHub Secrets) | ||
base64_token <- Sys.getenv("GOOGLE_OAUTH_TOKEN_BASE64") | ||
|
||
# Decode the Base64 string and write it to a binary RDS file | ||
decoded_token <- base64decode(base64_token) | ||
writeBin(decoded_token, oauth_token_path) | ||
} | ||
|
||
# Load the OAuth token from the RDS file and authenticate | ||
if (file.exists(oauth_token_path)) { | ||
token <- readRDS(oauth_token_path) | ||
gs4_auth(token = token) | ||
} else { | ||
print("OAuth token not found in directory.") | ||
gs4_auth(email, cache = ".secrets") | ||
} | ||
|
||
return() | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.