Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a token from `googleAuthR` in `googlesheets`

The googleAuthR is an R package which wraps the google API client library (authentication and further usage of the API). The googlesheets R package is used to integrate with the google sheets API (i.e., a wrapper around the google sheets API).

Each of these packages has a seperate OAuth2.0 process of their own. I'm using the googleAuthR in order to log into a shiny app, which in turn also uses the googlesheets library (but with a different authentication process).

The question: How can we set the googlesheets package to use the initial googleAuthR credentials as well?

Here is the process I'm using (within a shiny app):

For the googleAuthR login I'm using:

options(googleAuthR.webapp.client_secret = "***REMOVED_FROM_EXAMPLE***")
options(googleAuthR.webapp.client_id = "***REMOVED_FROM_EXAMPLE***")
options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/userinfo.email",
                                        "https://www.googleapis.com/auth/userinfo.profile",
                                        "https://www.googleapis.com/auth/spreadsheets"))

And for the googlesheets package, I'm currently using a pre-registered (separate) token which I saved into an RDS file, as suggested in the package's vignette:

suppressMessages(gs_auth(token = "googlesheets_token.rds", verbose = FALSE))
gsheet_log <- googlesheets::gs_url("https://docs.google.com/spreadsheets/d/***REMOVED_FROM_EXAMPLE***/edit#gid=0")

I would like a flow which replaces the use of gs_auth with the token generated by googleAuthR.

IMPORTANT NOTE (Added to this question at a later time):

I think that perhaps the use of googlesheets is relevant only for the short term and should be discouraged, because google are going to deprecate it on March 3, 2020 see message

Hence @Aurèle's comment might be the way to go here (hoping that googlesheets4 will get, eventually, all the capabilities of its predecessor). The question might still be interesting in the broader sense.

like image 623
Adi Sarid Avatar asked Oct 04 '19 12:10

Adi Sarid


1 Answers

I have struggled myself to authenticate via googleAuthR and then use my credentials to read a sheet using googlesheets4 library. Unfortunately, I still cannot make it work, but I found a crude workaround in "pure" googleAuthR. Using gar_api_generator function you can actually call any requests that is allowed in Google API.

library(shiny)
library(googleAuthR)
options(shiny.port = 8787)
options(googleAuthR.redirect = "http://localhost:8787")

# JSON with you client data from GCP
gar_set_client(scopes = "https://www.googleapis.com/auth/spreadsheets.readonly",
               web_json = "<YOUR_JSON>")
spreadsheet_key <- "<YOUR SHEET>"

read_googlesheet <- gar_api_generator(
  baseURI = "https://sheets.googleapis.com/v4/",
  http_header = 'GET',
  path_args = list(spreadsheets = spreadsheet_key,
                   values = "A:U"), #column range 
  data_parse_function = function(x) x$values
)

## ui.R
ui <- fluidPage(title = "googleAuthR Shiny Demo",
                tableOutput("gs")
)

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

  output$gs <- renderTable({
    df_raw <- read_googlesheet()
    # make the first row of the dataset as a header
    df <- df_raw[c(2:nrow(df_raw)), ]
    colnames(df) <- df_raw[1, ]
    df
  })
}

shinyApp(gar_shiny_ui(ui, login_ui = gar_shiny_login_ui), server)
like image 89
jjankowiak Avatar answered Sep 28 '22 17:09

jjankowiak