shinyapps.io googlesheet4 - Can't get Google credentials

Using googlesheets4/googledrive, could any one provide a more explicit example of how to create an authentication, save it somehow (whether in .secrets or elsewhere), and then call it in a non-interactive session.

Following this thread, I've made sure to limit the tokens in the gargle-auth folder to one. I've also tried saving the interactively-acquired authentication to the shinyapp deployment folder:

drive_auth(cache = 'explicit\path')

Then later calling

sheets_auth(cache = 'explicit\path', email = T)
#Or
sheets_auth(cache = 'explicit\path', email = 'account@gmail.com')

However, each time I try to deploy the app, I get the following error message

Error in value[[3L]](cond) : Can't get Google credentials.
Are you running googlesheets4 in a non-interactive session? Consider:
  * `sheets_deauth()` to prevent the attempt to get credentials.te code here

Unfortunately, it seems many of the vignettes available are for googlesheets only. I'm really in the weeds here, and have no option to make the sheets public due to data privacy rules. Appreciate any help.

library(shiny)
library(shinydashboard)
# library(googledrive)
library(googlesheets4)
library(tidyverse)


# googledrive::drive_auth(cache = 'C:/folders/gargle-auth', 
#                         email = T, scopes="https://www.googleapis.com/auth/spreadsheets.readonly")

sheets_auth(cache = 'C:/Users/qbt1/Desktop/sato app/gargle-auth', 
            scopes='https://www.googleapis.com/auth/spreadsheets.readonly',
            email = 'fakus@gmail.com')

ui <- dashboardPage(
  
  dashboardHeader(title = 'Title', titleWidth = 10),
  dashboardSidebar(disable = T),
  dashboardBody(skin = 'blue',
                box(width = 10,
                    actionButton(inputId = 'refresh', label = 'Refresh')
                    ),
                
                box(width = 10,
                    tableOutput('table')
                    )
                )
  
)

server <- function(input, output){
  
  observeEvent(input$refresh, {
    
    table <- read_sheet('####sheet_key#####') # Can get with drive_find(n_max=5)
    
    table_summary <- table %>% 
      group_by(some_col) %>% 
      summarize(
        n = sum_fun()
      )
    
    output$table_summary <- renderTable(summary, rownames = F)
    
  })
  
}

shinyApp(ui = ui, server = server)

Since posting - I've discovered this useful tutorial. Though it is for python, the same process works for R of enabling Google Drive/Sheets API, downloading json credentials, and then sharing the sheet with the service email given by google. You simply use sheets_auth(path = 'your_credentials.json'), which also works non-interactively after deploying the shiny app.

3 Likes