tidyverse/googlesheets4

"Can't get Google credentials" Error on Shinyapps.io

Opened this issue · 3 comments

I tried to follow instructions on post #184 by calling first gs4_auth(cache=".secrets") before deploying the app to shinyapps.io. In the system log, I can confirm the existence of cached token key file in ./secret folder. However, I still get "Can't get Google credentials." error on shinyapps.io. Thanks.

2023-12-20T15:31:42.518170+00:00 shinyapps[10831335]: gs4_auth(cache = ".secrets", email = TRUE, use_oob = TRUE)
2023-12-20T15:31:43.464681+00:00 shinyapps[10831335]: Error in gs4_auth(cache = ".secrets", email = TRUE, use_oob = TRUE) :
2023-12-20T15:31:43.468629+00:00 shinyapps[10831335]: Can't get Google credentials.
2023-12-20T15:31:43.472800+00:00 shinyapps[10831335]: ℹ Are you running googlesheets4 in a non-interactive session? Consider:
2023-12-20T15:31:43.477237+00:00 shinyapps[10831335]: • Call gs4_deauth() to prevent the attempt to get credentials.
2023-12-20T15:31:43.482349+00:00 shinyapps[10831335]: • Call gs4_auth() directly with all necessary specifics.
2023-12-20T15:31:43.486107+00:00 shinyapps[10831335]: ℹ See gargle's "Non-interactive auth" vignette for more details:
2023-12-20T15:31:43.489921+00:00 shinyapps[10831335]: ℹ https://gargle.r-lib.org/articles/non-interactive-auth.html
2023-12-20T15:31:43.493800+00:00 shinyapps[10831335]: Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
2023-12-20T15:31:43.497625+00:00 shinyapps[10831335]: Execution halted
2023-12-20T15:31:43.501376+00:00 shinyapps[10831335]: Shiny application exiting ...
2023-12-20T15:47:25.464647+00:00 shinyapps[10831335]: Container event from container-8999208: stop

Have you tried my fix?

gs4_auth(cache = ".secrets")

Use this for the first time running the app in R to get the OAuth token. When deploying the app to the server, upload this .secrets folder too. Now replace the code above with the code below and upload the app to the server. Use_oob=TRUE is basically what did the trick for me.

gs4_auth(cache = ".secrets", email = TRUE, use_oob = TRUE)

Originally posted by @comicalequation in #184 (comment)

#184 seems to date back 2020 and a lot has changed about Google auth since then. I suspect that you need to take active steps to make sure that the same OAuth client is being used in both places, in order to prevent the token cache miss.

In your app code, prior to the gs4_auth() call, execute this:

options(gargle_oauth_client_type = "installed")

Alternatively, locally, before you get the token you embed in the app, execute this:

options(gargle_oauth_client_type = "web")

and either don't set the option in your Shiny app or set it also to "web".

The problem probably comes from that fact that locally we have to default to an "installed app" flow, whereas on a server (which would include shinyapps.io), we have to default to a "web app" flow.

Thanks @jennybc , this works for me!

We recently hit a similar issue deploying an .Rmd to Posit Connect; when we tried to use the instructions in this gargle vignette we got this error message from Google. Adding options(gargle_oauth_client_type = "web") to our deployed code solved it. Thanks @jennybc!