Auto-build libraries for Google APIs with OAuth2 for both local and Shiny app use.

This guide is also available at the googleAuthR website

R Google API libraries using googleAuthR

Here is a list of available Google APIs to make with this library. The below libraries are all cross-compatible as they use googleAuthR for authentication backend e.g. can use just one OAuth2 login flow and can be used in multi-user Shiny apps.

Feel free to add your own via email or a pull request if you have used googleAuthR to build something cool.

googleAuthR has an R package generator which makes R package skeletons you can use to build your own Google API R package upon. Browse through the 154 options at this Github repository.

Example Shiny app

An example shiny app with Google authentication is deployed to shinyapps.io here. It uses the example app that is available in system.file("shiny", package="googleAuthR")

Thanks to:

Install

googleAuthR is available on CRAN:

install.packages("googleAuthR")

If you want to use the development version on Github, install via:

devtools::install_github("MarkEdmondson1234/googleAuthR")

Overview

This library allows you to authenticate easily via local use in an OAuth2 flow; within a Shiny app; or via service accounts. The main two functions are gar_auth() and gar_api_generator().

gar_auth - for authentication

This takes care of getting the authentication token, storing it and refreshing.

Use it before any call to a Google library.

gar_api_generator - for building an API

This creates functions for you to use to interact with Google APIs.

Use it within your own function definitions, to query the Google API you want.

Google API Setup

googleAuthR has a default project setup with APIs activated for several APIs, but it is recommended you use your own Client IDs as the login screen will be big and scary for users with so many APIs to approve.
It is preferred to configure your functions to only use the scopes they need. Scopes you need will be specified in the Google API documentation. Set scopes via the option googleAuthR.scopes.selected.

The below example sets scopes for Search Console, Google Analytics and Tag Manager:

options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/webmasters",
                                          "https://www.googleapis.com/auth/analytics",
                                          "https://www.googleapis.com/auth/tagmanager.readonly"))

Set up steps

  1. Set up your project in the Google API Console to use the Google API you want.

For local use

  1. Click ‘Create a new Client ID’, and choose “Installed Application”.
  2. Note your Client ID and secret.
  3. Modify these options after googleAuthR has been loaded:
  • options("googleAuthR.client_id" = "YOUR_CLIENT_ID")
  • options("googleAuthR.client_secret" = "YOUR_CLIENT_SECRET")

For Shiny use

  1. Click ‘Create a new Client ID’, and choose “Web Application”.
  2. Note your Client ID and secret.
  3. Add the URL of where your Shiny app will run, with no port number. e.g. https://mark.shinyapps.io/searchConsoleRDemo/
  4. And/Or also put in localhost or 127.0.0.1 with a port number for local testing. Remember the port number you use as you will need it later to launch the app e.g. http://127.0.0.1:1221
  5. In your Shiny script modify these options:
  • options("googleAuthR.webapp.client_id" = "YOUR_CLIENT_ID")
  • options("googleAuthR.webapp.client_secret" = "YOUR_CLIENT_SECRET")
  1. Run the app locally specifying the port number you used e.g. shiny::runApp(port=1221)
  2. Or deploy to your Shiny Server that deploys to web port (80 or 443).

Activate API

  1. Click on “APIs”
  2. Select and activate the API you want to use.
  3. Go to the documentation and find the API scope URL
  4. Set option in your R script for the scope e.g.
options("googleAuthR.scopes.selected" = 
      c("https://www.googleapis.com/auth/urlshortener"))