r-lib / gmailr

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

Problem on R 4.2.1? #173

Closed FJCC closed 1 year ago

FJCC commented 1 year ago

I have gmailr working on my laptop and a remote server, both running R 3.5.1. I am trying to implement gmailr on a server with R 4.2.1. I copied the json file and the token to the new server and now the hash value of the token does not match the token's file name. Could there be a problem with using gmailr on R 4.2.1?

Disclosure: I posted a question on the RStudio forum yesterday: https://community.rstudio.com/t/gmailr-token-name-does-not-match-hash/149625

> library(gmailr)
> options(gargle_verbosity = "debug")
> options(gargle_oauth_email = "fjcc.email@gmail.com")
> gm_auth_configure(path = "mail/MyApp2.json")
> gm_auth(path = "mail/MyApp2.json", cache = "mail")
trying `token_fetch()`
trying `credentials_service_account()`
JSON does not appear to represent a service account
Did you provide the JSON for an OAuth client instead of for a service account?
trying `credentials_external_account()`
aws.ec2metadata not installed; cant detect whether running on EC2 instance
trying `credentials_app_default()`
trying `credentials_gce()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_user_oauth2()`
Gargle2.0 initialize
adding "userinfo.email" scope
! Cache contains a token with a name that does not match its hash:
• "209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com" (name)
• 3596ec5a3e0c1801a184031exxxxxxxx_fjcc.email@gmail.com (hash)

After the warning about the mismatch between the name and the hash, the file is renamed but the authentication process fails.

Loading the token into R using readRDS and running its hash() function shows the same problem on the new server (server3) but not locally or on the older server (server2).

#On my laptop (Linux Mint 18.3, R 3.5.1)
> TOKEN <- readRDS("mail/209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com")
> TOKEN$hash()
[1] "209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com"

#scp the files to the two servers

fjcc$ scp mail/209fa3fd00668f622046dcf6xxxxxxxx\_fjcc.email@gmail.com  username@server3:~/R/
209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com                                                       100% 7256     7.1KB/s   00:00    
fjcc$ scp mail/209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com  username@server2:~/R/
209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com                                                       100% 7256     7.1KB/s   00:00

#Check the hash on server2 (Ubuntu 16.04, R 3.5.1)
> TOKEN2 <- readRDS("209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com")
> TOKEN2$hash()
[1] "209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com"

#Check the hash on server3 (Ubuntu 22.04, R 4.2.1)
> TOKEN3 <- readRDS("209fa3fd00668f622046dcf6xxxxxxxx_fjcc.email@gmail.com")
> TOKEN3$hash()
[1] "3596ec5a3e0c1801a184031exxxxxxxx_fjcc.email@gmail.com"
jennybc commented 1 year ago

This is a known phenomenon that I have notes on in gargle. The $hash() method calls base::serialize() and on R 3.5 it uses version = 2, whereas on R >= 3.6 it uses version = 3. This is all happening in httr, so it's not something I can easily change. So the short answer is that you're going to see this warning in this case. The renaming of the token file should get you back into business.

... the authentication process fails

Can you say more about this failure?

FJCC commented 1 year ago

Thanks for your quick response! The full gm_auth() output is below. I kill the process with CTRL +C

> gm_auth(path = "mail/MyApp2.json", cache = "mail")
trying `token_fetch()`
trying `credentials_service_account()`
JSON does not appear to represent a service account
Did you provide the JSON for an OAuth client instead of for a service account?
trying `credentials_external_account()`
aws.ec2metadata not installed; can't detect whether running on EC2 instance
trying `credentials_app_default()`
trying `credentials_gce()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_user_oauth2()`
Gargle2.0 initialize
adding "userinfo.email" scope
! Cache contains a token with a name that does not match its hash:
• "209fa3fd00668f622046dcf6f9802705_fjcc.apache@gmail.com" (name)
• 3596ec5a3e0c1801a184031e547501e1_fjcc.apache@gmail.com (hash)
  Will attempt to repair by renaming
loading token from the cache
no matching token in the cache
initiating new token
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
/usr/bin/xdg-open: line 882: www-browser: command not found
/usr/bin/xdg-open: line 882: links2: command not found
/usr/bin/xdg-open: line 882: elinks: command not found
/usr/bin/xdg-open: line 882: links: command not found
/usr/bin/xdg-open: line 882: lynx: command not found
/usr/bin/xdg-open: line 882: w3m: command not found
xdg-open: no method available for opening 'https://accounts.google.com/o/oauth2/v2/auth?client_id=72302230830-hipt3m02ngtf65otisia9uvdvokiiho2.apps.googleusercontent.com&scope=https%3A%2F%2Fmail.google.com%2F%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=http%3A%2F%2Flocalhost%3A1410%2F&response_type=code&state=fJsowSioaO'
jennybc commented 1 year ago

I think your error is that you are providing the path to the JSON for an OAuth client via gm_auth(path =), whereas the path argument of gm_auth() is meant to take the filepath to a service account.

I think you mean to use gm_auth_configure(path = "path/to/downloaded/json") first, then gm_auth(cache = "mail").

FJCC commented 1 year ago

I rearranged my files to conform to the usual practice of having the json and the token in different directories. The set up I had works on my other systems but I'm well past the point where I'm willing to accept magic. The json is now in the mail folder and the token is in mail/tokens. Unfortunately, using the path argument only in gm_auth_configure() did not change the behavior. I would very much appreciate any other suggestions but I understand it isn't your job to fix my unique problem.

> library(gmailr)

Attaching package: ‘gmailr’

The following object is masked from ‘package:utils’:

    history

The following objects are masked from ‘package:base’:

    body, date, labels, message

> options(gargle_verbosity = "debug")
> gm_auth_configure(path = "mail/MyApp2.json")
> gm_auth(cache = "mail/tokens")
trying `token_fetch()`
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()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_user_oauth2()`
Gargle2.0 initialize
adding "userinfo.email" scope
! Cache contains a token with a name that does not match its hash:
• "209fa3fd00668f622046dcf6f9802705_fjcc.apache@gmail.com" (name)
• 3596ec5a3e0c1801a184031e547501e1_fjcc.apache@gmail.com (hash)
  Will attempt to repair by renaming
loading token from the cache
no matching token in the cache
initiating new token
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
/usr/bin/xdg-open: line 882: www-browser: command not found
/usr/bin/xdg-open: line 882: links2: command not found
/usr/bin/xdg-open: line 882: elinks: command not found
/usr/bin/xdg-open: line 882: links: command not found
/usr/bin/xdg-open: line 882: lynx: command not found
/usr/bin/xdg-open: line 882: w3m: command not found
xdg-open: no method available for opening 'https://accounts.google.com/o/oauth2/v2/auth?client_id=72302230830-hipt3m02ngtf65otisia9uvdvokiiho2.apps.googleusercontent.com&scope=https%3A%2F%2Fmail.google.com%2F%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=http%3A%2F%2Flocalhost%3A1410%2F&response_type=code&state=V3Dz5zqgoC'
jennybc commented 1 year ago

OK, this still looks like you are trying to port a token across the R 3.5 vs R >= 3.5 divide, though.

FJCC commented 1 year ago

Yes, I have not updated the local R. I was holding off on that to stay aligned with the old production server and because I understood that the changing of the file name should have handled the change in serialize(). I'll upgrade so I can get past this problem, I hope. I'll report back on whether the upgrade is all that is needed. Thank you very much for spending time on this.

jennybc commented 1 year ago

I think you need to get both systems on R >= 3.5 AND you needed to make that change where you provide the path to the client JSON in gm_auth_configure() (instead of to gm_auth()).

FJCC commented 1 year ago

The update to R 4.2.1 on my local system fixed the problem. I'm sorry for causing needless trouble.