tidyverse / googlesheets4

Google Spreadsheets R API (reboot of the googlesheets package)
https://googlesheets4.tidyverse.org
Other
360 stars 53 forks source link

Problem with `gs4_auth()` and `gs4_deauth()` #305

Open toxintoxin opened 11 months ago

toxintoxin commented 11 months ago

Sorry, this is my first time using your package, and I found some guide for myself, but I meet a primary trouble that I can't get into my google sheet.
My ultimate goal is to deploy the app on shinyapps.io, and everyone without google account could edit my Google sheet.
First, I follow this guide https://stackoverflow.com/a/70215575/22331901, it seems to meets my requirement.
But, I failed in gs4_auth(), it did open my edge, and edge returns Authentication complete. Please close this page and return to R. Then, in the console, it returns

> library(googlesheets4)
> options(gargle_oauth_cache = ".secrets")
> gs4_auth()
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in `gs4_auth()`:
! Can't get Google credentials.
ℹ Are you running googlesheets4 in a non-interactive session? Consider:
• Call `gs4_deauth()` to prevent the attempt to get credentials.
• Call `gs4_auth()` directly with all necessary specifics.
ℹ See gargle's "Non-interactive auth" vignette for more details:
ℹ <https://gargle.r-lib.org/articles/non-interactive-auth.html>
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/googlesheets4_error>
Error in `gs4_auth()`:
! Can't get Google credentials.
ℹ Are you running googlesheets4 in a non-interactive session? Consider:
• Call `gs4_deauth()` to prevent the attempt to get credentials.
• Call `gs4_auth()` directly with all necessary specifics.
ℹ See gargle's "Non-interactive auth" vignette for more details:
ℹ <https://gargle.r-lib.org/articles/non-interactive-auth.html>
---
Backtrace:
    ▆
 1. └─googlesheets4::gs4_auth()
Run rlang::last_trace(drop = FALSE) to see 3 hidden frames.
> rlang::last_trace(drop = FALSE)
<error/googlesheets4_error>
Error in `gs4_auth()`:
! Can't get Google credentials.
ℹ Are you running googlesheets4 in a non-interactive session? Consider:
• Call `gs4_deauth()` to prevent the attempt to get credentials.
• Call `gs4_auth()` directly with all necessary specifics.
ℹ See gargle's "Non-interactive auth" vignette for more details:
ℹ <https://gargle.r-lib.org/articles/non-interactive-auth.html>
---
Backtrace:
    ▆
 1. └─googlesheets4::gs4_auth()
 2.   └─googlesheets4:::gs4_abort(...)
 3.     └─cli::cli_abort(...)
 4.       └─rlang::abort(...)

I tried any answer on stackoverflow, all failed, nothing changed.

  1. update curl
  2. update gargle
  3. install googlesheets4 from github

Furthermore, gs_deauth() even also didn't work

> gs4_deauth()
> ss <- "https://docs.google.com/spreadsheets/d/1Cw9YSvzsGW5JDFptZ4-qVE0veNthwU3lxsZErk1So5E/edit?usp=sharing"
> dat <- read_sheet(ss)
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Timeout was reached: [sheets.googleapis.com] Failed to connect to sheets.googleapis.com port 443 after 10009 ms: Timeout was reached

Is it possible that this is due to the internet? I'm in China and I'm using SSR

jennybc commented 11 months ago

The experience and code you show above ... is that during the initial phase, where you are trying to get your first token? I.e. you are still working locally and interactively.

Or does it describe something that's happening on the server where you hope to deploy this app?

Regardless, you should read this article on some things you can do while troubleshooting auth, so that you get more information.

https://gargle.r-lib.org/articles/troubleshooting.html

toxintoxin commented 11 months ago

Yes, I ran into trouble almost at the beginning, and I run the above code locally.
I got these after set op <- options(gargle_verbosity = "debug"), without open a browser

trying `token_fetch()`
Trying `credentials_byo_oauth()` ...
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_service_account()`
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_external_account()`
aws.ec2metadata not installed; can't detect whether running on EC2 instance
trying `credentials_app_default()`
Trying `credentials_gce()` ...
✖ We don't seem to be on GCE.
trying `credentials_user_oauth2()`
attempt to access internal gargle data from: googlesheets4
Gargle2.0 initialize
adding "userinfo.email" scope
loading token from the cache
Error caught by `token_fetch()`:
attempt to set an attribute on NULL
Error in `gs4_auth()`:
! Can't get Google credentials.
ℹ Are you running googlesheets4 in a non-interactive session? Consider:
• Call `gs4_deauth()` to prevent the attempt to get credentials.
• Call `gs4_auth()` directly with all necessary specifics.
ℹ See gargle's "Non-interactive auth" vignette for more details:
ℹ <https://gargle.r-lib.org/articles/non-interactive-auth.html>
Run `rlang::last_trace()` to see where the error occurred.
jennybc commented 11 months ago

Yes, I ran into trouble almost at the beginning

Can you be extremely precise? Have you ever successfully gotten a token as an interactive user?

I'm trying to reconcile what you're saying with this:

loading token from the cache

which suggests a token is already in the cache (?)

toxintoxin commented 11 months ago

This is the first time I've used your package and I've never been successful.
Because I learned that googlesheets4 is probably the most convenient persistent data storage work flow with shinyapps.io, and after much googling I found https://stackoverflow.com/a/70215575/22331901 this guide to be the most reliable. I started directly based on it. Then

> install.packages("googlesheets4")
> library(googlesheets4)
> options(gargle_oauth_cache = ".secrets")
> gs4_auth()

Then I ran into the trouble I mentioned and I started struggling with it.

I have checked, nothing in /.secrets

> list.files("/.secrets")
character(0)

And I have never got a token, that folder is empty

> gargle_oauth_sitrep()
ℹ Reporting the default cache location.
0 tokens found in this gargle OAuth cache:
C:/Users/someo/AppData/Local/gargle/gargle/Cache
jennybc commented 11 months ago

I would recommend trying to use the package interactively, taking no control of auth, and seeing if the entry-level flow works. But I am beginning to suspect it's related to this:

Is it possible that this is due to the internet? I'm in China and I'm using SSR

toxintoxin commented 11 months ago

Thank you for your help, it is indeed a network problem, I tried different nodes and they all failed until I switched to another service provider

toxintoxin commented 11 months ago

I deployed the folder .secrets to shinyapps.io along with my app.R. Although I can access shinyapps.io in China but I can't seem to complete the authorization, which is really annoying and I can't ask all my colleagues to master the ability to access google. :(