2

I'm currently working with an R Shiny App that utilizes googlesheets4 to read data in from a GoogleSheet. I recognize that authentication is important for accessing GoogleSheets, so I've tried authenticating the app using the code below in my app.R file (email and api key removed for privacy):

  ### AUTHENTICATE FOR GOOGLE SHEETS ACCESS ###
  sheets_auth(
  email = "MY EMAIL",
  path = NULL,
  scopes = "https://www.googleapis.com/auth/spreadsheets",
  cache = gargle::gargle_oauth_cache(),
  use_oob = gargle::gargle_oob_default(),
  token = "MY API KEY"
  )

When I run the dashboard locally with this code, it first loads a webpage asking that sign in to Google and authenticate Tidyverse's API: screenshot of Tidyverse API.... After I sign-in, the app opens in a separate window perfectly.

However, I don't want users to have to authenticate anything when using my dashboard once it's published. Ideally I would like the necessary information included in the Shiny code to allow for the app to authenticate access to the GoogleSheet on its own.

I am very new to googlesheets4, and APIs in general, so appreciate any and all guidance with getting the correct credentials into my app.R file.

Thank you!


UPDATE...4.23.20

After utilizing https://gargle.r-lib.org/articles/non-interactive-auth.html#provide-a-service-account-token-directly

I have been able to get this nearly worked out. The final hurdle is at the publishing phase, where I'm getting this error upon app launch:

Error: .auth not found Code as I have it now:

### AUTHENTICATE FOR GOOGLE SHEETS ACCESS ###
library(gargle)
library(googleAuthR)
library(searchConsoleR)
library(googledrive)
# Approach #1: use an option.
# Either specify the user:
options(gargle_oauth_email = "MY EMAIL")

# INSERT JSON FILE FROM GOOGLE API SERVICE ACCOUNT
drive_auth(path = "FTRjsonGoogleAPI.json")
options(gargle_quiet = FALSE)
zx8754
  • 52,746
  • 12
  • 114
  • 209
hlag
  • 21
  • 3
  • This resource may help: https://gargle.r-lib.org/articles/non-interactive-auth.html – bs93 Apr 22 '20 at 23:07
  • 1
    Try using `gs4_token` to generate the authentication file: https://googlesheets4.tidyverse.org/reference/gs4_token.html – nniloc Apr 22 '20 at 23:15

1 Answers1

2

What works for me is saving the token in a .secrets file that gets uploaded when deploying the shiny app.

The code is this:

library(googledrive)
library(googlesheets4)

# Google sheets authentification -----------------------------------------------
options(gargle_oauth_cache = ".secrets")
drive_auth(cache = ".secrets", email = "youremail@gmail.com")
gs4_auth(token = drive_token())

You need to run this once locally to sign in. It will check for a .secrets file and either read it in or ask you to sign in and store the token. This .secrets file can then be uploaded together with the rest of your app.

Nikos Bosse
  • 144
  • 11
  • When I run: 1. options(gargle_oauth_cache = ".secrets") 2. gs4_auth(cache = ".secrets", email = "placeholderemail@...") 3. gs4_auth(token = gs4_token()) -- I don't see a .secrets file created, is there something I need to do to see the file? – Liam Haller Jun 02 '23 at 14:39
  • 1
    From memory I think you have to create the file yourself manually (akin to a text file) and then just call it .secrets – Nikos Bosse Jun 05 '23 at 12:20