r-lib / gmailr

Access the Gmail RESTful API from R.
https://gmailr.r-lib.org
Other
229 stars 56 forks source link

Authentication expiring repetitively for non-interactive auth #192

Closed estebangatillo closed 10 months ago

estebangatillo commented 1 year ago

I have successfully used gmailr to download attachments from gmail (thank you!). Getting the authentication up was troublesome, but it's working. However, non-interactively my token keeps expiring, I have to keep generating new auth keys interactively, and it's a hassle. I double checked the app and there doesn't appear to be any limitations, so I can't tell why I'm getting an expiration message.

Here is the error:

'/usr/lib/R/bin/R --no-echo --no-restore --file=./R/download-gmail-attachment.R'

! Using an auto-discovered, cached token.
  To suppress this message, modify your code or options to clearly consent to the use of a cached token.
  See gargle's "Non-interactive auth" vignette for more details:
  <https://gargle.r-lib.org/articles/non-interactive-auth.html>
ℹ The gmailr package is using a cached token for sm@capastrategies.com.
Auto-refreshing stale OAuth token.
Error in itr(...) : Unauthorized (HTTP 401).
Calls: gm_messages -> page_and_trim -> itr -> stop_for_status
In addition: Warning message:
Unable to refresh token: invalid_grant
• Token has been expired or revoked. 

Based on the issues I've read about, I've added this to my script:

options(gargle_oauth_email = "my.email@gmail.com")

And I think this is where the non-interactive issue is occurring:

gm_auth_configure(path = path, email = "capa.uploader@gmail.com", scopes = "full")

I've also fiddled with using just this with no results:

gm_auth(email = "my.email@gmail.com", scopes = "full")

Here is my session info:

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gmailr_1.0.1

loaded via a namespace (and not attached):
 [1] httr_1.4.6      compiler_4.1.2  magrittr_2.0.3  R6_2.5.1       
 [5] gargle_1.4.0    cli_3.6.1       fs_1.5.2        base64enc_0.1-3
 [9] glue_1.6.2      lifecycle_1.0.3 rlang_1.1.1    
jennybc commented 1 year ago

What is the publishing status of your OAuth client? If it's "Testing", refresh tokens will become invalid (unrefreshable) every 7 days.

https://gmailr.r-lib.org/articles/oauth-client.html#use-a-non-verified-in-production-app

estebangatillo commented 1 year ago

OMG, I thought I was barking up the wrong tree when it told me to verify with a youtube video. Seriously? Then I read this and I LOVE how you wrote it!

"Presumably you have no intention of doing any of these things, since this OAuth client is for personal use. Regardless, click “CONFIRM” to proceed.

Now the OAuth consent screen settings reflect the new publishing status, which is “In production”. However, you will also see that your project “Needs verification”. You will just have to accept this situation as something awkward that comes when an individual R user is forced to use API configuration that is really designed for application developers."

Thank you much for the link! I'll work through this.