Skip to contents

The main goal we want to do is authorize programmatic access to the google drive. When we use googledrive in an interactive session, it will prompt us to authorize access. Our steps therefore are: 1. authorize access 2. save the “token” that authenticates us for access 3. encrypt the token 4. save the decryption key as an environmental variable

Results: * The encrypted token can then be shared without concern. * The decryption key can live as a github secret that enables github actions to use the token for access

drive_auth_configure(path = ".secrets/gdrive-oauth.json")
drive_auth(email = TRUE, scopes = "https://www.googleapis.com/auth/drive", cache = FALSE)

my_token <- drive_token()
key <- gargle::secret_make_key()
cat(paste0("GDRIVE_KEY=", key)) # copy the result of this string
usethis::edit_r_environ() # run this to open your .Renviron file

# paste the result into that file
# restart your R session
dir.create(".secrets")

gargle::secret_write_rds(
    x = my_token, 
    path = ".secrets/gdrive-token.rds", 
    key = "GDRIVE_KEY"
)

# check that the token can be decrypted
stopifnot(!is.null(decrypt_gdrive_token()))