Google authentication types for R

Other more modern libraries

googleAuthR was one of my first R packages and has enjoyed 7+ years of being my key workhorse for Google authentication, but in the meantime more modern packages have been released that may be more suited to your needs - consider gargle (that googleAuthR heavily depends upon now for authentication rather than its own home spun functions) and firebase for alternatives. The roles/functionality of googleAuthR aside authentication such as batching, package creation etc. will eventually be also superseded by smaller packages. However, googleAuthR is still in active use and will be supported in a maintenance mode.

Version 2.0

Version 2.0 removed googleAuthR shiny modules in favour of gar_shiny_*. If you need those older legacy functions depend on googleAuthR == 1.4.1 or before.

Quick user based authentication

Once setup, then you should go through the Google login flow in your browser when you run this command:

# starts auth process with defaults
#>The googleAuthR package is requesting access to your Google account. Select a 
#> pre-authorised account or enter '0' to obtain a new token. Press Esc/Ctrl + C to abort.

#> 1:
#> 2:

The authentication cache token is kept at a global level as per the gargle library documentation - see there for more details.

You can also specify your email to avoid the interactive menu:

gar_auth(email = "")

These functions are usually wrapped in package specific functions when used in other packages, such as googleAnalyticsR::ga_auth()

Client options

Most libraries will set the appropriate options for you, otherwise you will need to supply them from the Google Cloud console, in its APIs & services > Credentials section ( ).

You will need as a minimum:

If creating your own library you can choose to supply some or all of the above to the end-user, as an end-user you may need to set some of the above (most usually your own user authentication).

Multiple authentication tokens

googleAuthR > 1.0.0

Authentication cache tokens are kept at a global level on your computer. When you authenticate the first time with a new, scope or email then you will go through the authentication process in the browser, however the next time it wil be cached and be a lot quicker.

# switching between auth scopes
# first time new scope manual auth, then auto if supplied email   
gar_auth(email = "", 
         scopes = "")

# ... query Google Drive functions ...

gar_auth(email = "", 
         scopes = "")

# ..query BigQuery functions ...

Setting the client via Google Cloud client JSON

To avoid keeping track of which client_id/secret to use, Google offers a client ID JSON file you can download from the Google Cloud console here - Make sure the client ID type is Desktop for desktop applications.

You can use this to set the client details before your first authentication. The above example would then be:


# set the scopes required
scopes = c("", 

# set the client
gar_set_client("client-id.json", scopes = scopes)

# authenticate and go through the OAuth2 flow first time

# can run Google Analytics API calls:

# and run Search Console API calls:

You can also place the file location of your client ID JSON in the GAR_CLIENT_JSON environment argument, where it will look for it by default:

# .Renviron

Then you just need to supply the scopes:

gar_set_client(scopes = "")

Authentication with no browser

Refer to this gargle article on how to authenticate in a non-interactive manner

Authentication with a JSON file via Service Accounts

You can also authenticate single users via a server side JSON file rather than going through the online OAuth2 flow. The end user could supply this JSON file, or you can upload your own JSON file to your applications. This is generally more secure if you know its only one user on the service, such as for Cloud services.

This involves downloading a secret JSON key with the authentication details. More details are available from Google here: Using OAuth2.0 for Server to Server Applications[]

To use, go to your Project in the Google Developement Console and select JSON Key type. Save the JSON file to your computer and supply the file location to the function gar_auth_service()


Roles all start with roles/* e.g. roles/editor - a list of predefined roles are here or you can see roles within your GCP console here.

Creating service account and a key

From R

The gar_service_create() and related functions let you create service accounts from a user OAuth2 login. The user requires permission iam.serviceAccounts.create for the project. Most often the user is an Owner/Editor.

The workflow for authenticating with a new key from R is:

See this related Google help article or creating service accounts.

The above workflow is encapsulated within gar_service_provision() which will run through them for you if you supply it with your GCP projects Client Id (another JSON file that identifies your project.)


Navigate to the JSON file from the Google Developer Console via: Credentials > New credentials > Service account Key > Select service account > Key type = JSON

If you are using the JSON file, you must ensure:

An example using a service account JSON file for authentication is shown below:

options(googleAuthR.scopes.selected = "")
service_token <- gar_auth_service(json_file="~/location/of/the/json/secret.json")
analytics_url <- function(shortUrl, 
                          timespan = c("allTime", "month", "week","day","twoHours")){

  timespan <- match.arg(timespan)

  f <- gar_api_generator("",
                         pars_args = list(shortUrl = "shortUrl",
                                          projection = "FULL"),
                         data_parse_function = function(x) { 
                           a <- x$analytics 

  f(pars_arguments = list(shortUrl = shortUrl))

Another example is from the searchConsoleR library - in this case we avoid using scr_auth() to authenticate via the JSON, which has had the service email added to the Search Console web property as a user.

options(googleAuthR.scopes.selected = "") 



Authentication within Shiny

If you want to create a Shiny app just using your data, refer to the non-interactive authentication article on gargle

If you want to make a multi-user Shiny app, where users login to their own Google account and the app works with their data, googleAuthR provides the below functions to help make the Google login process as easy as possible.

Types of Shiny Authentication

There are now these types of logins available, which suit different needs:

Shiny Modules

googleAuthR uses Shiny Modules. This means less code and the ability to have multiple login buttons on the same app.

To use modules, you need to use the functions ending with _UI in your ui.R, then call the id you set there server side with the callModule(moduleName, "id") syntax. See the examples below.

Shiny Authentication Examples

Remember that client IDs and secrets will need to be created for the examples below. You need to pick a clientID for web applications, not "Other" as is used for offline googleAuthR functions.

URL redirects

In some platforms the URL you are authenticating from will not match the Docker container the script is running in (e.g. or a kubernetes cluster) - in that case you can manually set it via options(googleAuthR.redirect = In other circumstances the Shiny app should be able to detect this itself.

gar_shiny_* functions example

This uses the most modern gar_shiny_* family of functions to create authentication. The app lists the files you have stored in Google Drive.

gar_set_client(scopes = "")

fileSearch <- function(query) {
                    data_parse_function = function(x) x$files)()

## ui.R
ui <- fluidPage(title = "googleAuthR Shiny Demo",
                          label = "Google Drive query", 
                          value = "mimeType != 'application/'"),

## server.R
server <- function(input, output, session){

  # create a non-reactive access_token as we should never get past this if not authenticated

  output$gdrive <- renderTable({

    # no need for with_shiny()


shinyApp(gar_shiny_ui(ui, login_ui = gar_shiny_login_ui), server)

googleSignIn module example

This module is suitable if you don't need to authenticate APIs in your app, you just would like a login. You can then reach the user email, id, name or avatar to decide which content you want to show with durther logic within your Shiny app.

You only need to set the for this login, as no secrets are being created.


options(googleAuthR.webapp.client_id = "")

ui <- fluidPage(

    titlePanel("Sample Google Sign-In"),


        with(tags, dl(dt("Name"), dd(textOutput("g_name")),
                      dt("Email"), dd(textOutput("g_email")),
                      dt("Image"), dd(uiOutput("g_image")) ))

server <- function(input, output, session) {

  sign_ins <- shiny::callModule(googleSignIn, "demo")

  output$g_name = renderText({ sign_ins()$name })
  output$g_email = renderText({ sign_ins()$email })
  output$g_image = renderUI({ img(src=sign_ins()$image) })


# Run the application 
shinyApp(ui = ui, server = server)


Auto-authentication can be performed upon a package load.

This requires the setup of environment variables either in your .Renviron file or via Sys.setenv() to point to a previously created authentication file. This file can be either a .httr-oauth file created via gar_auth() or a Google service account JSON downloaded from the Google API console.

This file will then be used for authentication via gar_auth_auto. You can call this function yourself in scripts or R sessions, but its main intention is to be called in the .onAttach function via gar_attach_auth_auto, so that you will authenticate right after you load the library via library(yourlibrary)

An example from googleCloudStorageR is shown below:

.onAttach <- function(libname, pkgname){

                                    environment_var = "GCS_AUTH_FILE")

..which calls an environment variable set in ~/.Renvion:


Revoking Authentication

For local use, call gar_deauth() to de-authenticate a session. To avoid cache tokens being reused delete them from the gargle cache folder, usually ~/.R/gargle/gargle-oauth/

For service level accounts delete the JSON file.

For a Shiny app, a cookie is left by Google that will mean a faster login next time a user uses the app with no Authorization screen that they get the first time through. To force this every time, activate the parameter revoke=TRUE within the googleAuth function.

Try the googleAuthR package in your browser

Any scripts or data that you put into this service are public.

googleAuthR documentation built on Jan. 28, 2022, 5:06 p.m.