Here we describe how to do auth in a package that uses gargle, without requiring any user interaction.

Provide a token or pre-authorize token discovery

The main principle for auth that does not require user interaction:

Provide a token directly or take advance measures that indicate you want a token to be discovered.

Here are several ways to achieve this, basically in order of preference.

Provide a service account token directly

When two computers are talking to each other, possibly with no human involvement, the most appropriate type of token to use is a service account token.

This requires some advance preparation, but that tends to pay off pretty quickly, in terms of having a much more robust auth setup.

Step 1: Get a service account and then download a token. Described in the gargle article How to get your own API credentials, specifically in the Service account token section.

Step 2: Call the wrapper package’s main auth function proactively and provide the path to your service account token. Example using googledrive:

If this code is running on, e.g., a continuous integration service and you need to use an encrypted token, see the gargle article Managing tokens securely.

Rig a service account token as Application Default Credentials

Wrapper packages that use gargle::token_fetch() in the recommended way have access to the token search strategy known as Application Default Credentials.

You need to put your service token in a very specific location or, alternatively, record the location of your service token in a specific environment variable.

Full details are in the credentials_app_default() section of the gargle article How gargle gets tokens.

If you have your token rigged properly, you do not need to do anything else, i.e. you do not need to call PACKAGE_auth() explicitly. Your token should just get discovered upon first need.

For troubleshooting purposes, you can temporarily toggle a gargle option to see verbose output about the execution of gargle::token_fetch():

options(gargle_quiet = FALSE)

Provide an OAuth token directly

If you somehow have the OAuth token you want to use as an R object, you can provide it directly to the token argument of the main auth function. Example using googledrive:

gargle caches each OAuth token it obtains to an .rds file, by default. If you know the filepath to the token you want to use, you could use readRDS() to read it and provide as the token argument to the wrapper’s auth function. Example using googledrive:

# googledrive
drive_auth(token = readRDS("/path/to/your/oauth-token.rds"))

How would you know this filepath? That requires some attention to the location of gargle’s OAuth token cache folder, which is described in the next section.

Full details are in the credentials_byo_oauth2() section of the gargle article How gargle gets tokens.

Allow an OAuth token to be discovered

This is the least recommended strategy, but it appeals to many users, because it doesn’t require creating a service account. Just remember that the perceived ease of using the token you already have (an OAuth user token) is quickly cancelled out by the greater difficulty of managing such tokens for non-interactive use.

Two main principles:

  1. Take charge of – or at least notice – the folder where OAuth tokens are being cached.
  2. Make sure exactly one cached token will be identified and pre-authorize its use.

There are many ways to do this. We’ll show a specific example that should convey the general idea.

Create a cache for a specific project

Step 1: Obtain the token intended for non-interactive use and make sure it’s cached in a (hidden) directory of the current project. Using gcalendr as an example:

You do this once per project.

Step 2: Announce the location of the cache and pre-authorize the use of a suitable token discovered there. Continuing the gcalendr example:

This is needed in any script, .Rmd, or app that needs to use the token non-interactively. Be very intentional about with paths and working directory. Personally I would use here::here(".secrets)" everywhere above, to make things more robust.