To be placed within .onAttach to auto load an authentication file from an environment variable.

gar_attach_auto_auth(required_scopes, environment_var = "GAR_AUTH_FILE",
  travis_environment_var = "TRAVIS_GAR_AUTH_FILE")



A character vector of minimum required scopes for this API library


The name of the environment variable where the filepath to the authentication file is kept


Name of Travis environment var that contains auth file path

This function works with gar_auto_auth. It is intended to be placed within the .onAttach hook so that it loads when you load your library.

For auto-authentication to work, the environment variable needs to hold a file path to an existing auth file such as created via gar_auth or a JSON file file download from the Google API console.


Invisible, used for its side effects of calling auto-authentication.


If you are using Travis to make tests, then a specific environment name for a Travis auth file is also needed, that should be relative to the home directory of your Github repository.

You should then also encrypt the auth file and include the encrypted file in your Github repository. See googlesheets vignette on managing auth tokens and Travis encrypting files how-to for background.

To work on travis, you will need one auth token for the library load in the home folder, and another in the testthat folder. You can achieve this by using the same encrypted file command twice within your .travis.yml configuration, writing out to the two different folders: openssl aes-256-cbc -K $encrypted_0a6446eb3ae3_key -iv $encrypted_0a6446eb3ae3_key -in auth.json.enc -out auth.json -d openssl aes-256-cbc -K $encrypted_0a6446eb3ae3_key -iv $encrypted_0a6446eb3ae3_key -in auth.json.enc -out tests/testthat/auth.json -d

See also

Other authentication functions: gar_auth_service, gar_auth, gar_auto_auth, gar_gce_auth, get_google_token, is_legit_token, token_exists


not_run({ .onAttach <- function(libname, pkgname){ googleAuthR::gar_attach_auto_auth("", "US_AUTH_FILE") } ## will only work if you have US_AUTH_FILE environment variable pointing to an auth file location ## .Renviron example US_AUTH_FILE="/home/mark/auth/urlshortnerauth.json" })