rgoogle-sheetsshinydeploymentgoogle-oauth

Problems with ShinyApp and Google authentification, when making Public


I have a ShinyApp that should communicate (read and write) with a Google sheet. In local environment the code is working perfectly but when I deploy the app the google authentification with .json seems not to work. Shinyapp.io log:

Error in gs4_auth("client_secret_mysecret.apps.googleusercontent.com.json") : Can't get Google credentials.

I have tried everything I could find there and in the documentation (I may have done certain things wrong) but I have not found a solution. Maybe someone has the same problem. Here is a part of my code: (if you need more, please tell me)

# Path to  service account JSON file 
json_path <- "my-json-fil.json"  # Update this path

# Authenticate using the service account JSON file
gs4_auth(path = json_path, cache = FALSE)

# Google Sheets URL or ID 
sheet_url <- "https://docs.google.com/spreadsheets/d/1wqsOXhZawPhX59N2n2c1rt7dLQfHurbxwlIX87QpJk4/edit"

# Player list
players <- c("Spieler1", "Spieler2", "Spieler3", "Spieler4")

# Define training dates
training_dates <- seq(as.Date("2024-07-22"), by = "7 days", length.out = 20)

training_dates <- sort(c(training_dates, seq(as.Date("2024-07-24"), by = "7 days", 
length.out = 20)))  # Every Wednesday

end_date <- as.Date("2025-04-15")

# Function to load data from Google Sheets
load_attendance_data <- function(sheet_url) {
  tryCatch(    {
  gs4_get(sheet_url) %>%
    read_sheet() %>%
    mutate(Datum = as.Date(Datum))
},
error = function(e) {
  showNotification("Fehler beim Laden der Daten. Verwende lokale Daten.", type = "error")
  data.frame(Spieler = rep(players, each = length(training_dates)),
             Datum = rep(training_dates, times = length(players)),
             Anwesend = FALSE)
    }
  )
}

# Function to save data to Google Sheets
save_attendance_data <- function(data, sheet_url) {
 tryCatch(
{
  write_sheet(data, sheet_url, sheet = 1)
  showNotification("Daten erfolgreich gespeichert!", type = "message")
},
error = function(e) {
  showNotification(paste("Fehler beim Speichern der Daten:", e$message), type = 
"error")
    }
  )
}

I would be very happy if someone could help me. Thanks


Solution

  • I found the error. There is as well the authentification with google drive needed:

    drive_auth(path = json_path)