r-lib / gargle

Infrastructure for calling Google APIs from R, including auth
https://gargle.r-lib.org/
Other
113 stars 33 forks source link

More informative error when problem with a json key detected #209

Closed dokato closed 2 years ago

dokato commented 2 years ago

Recently, I had some changes to my GCP account, hence it reseted all my API keys etc.

Which is fine as I regenerated my service account token with these instructions. But! The error I was getting in my app that uses googlesheets4 was really puzzling. First of all, after you call sth along the lines:

gargle::token_fetch(path = 'new_api_key.json')

it takes you to the interactive authentication, which on remote server might not be available:

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
/usr/bin/xdg-open: 869: www-browser: not found
/usr/bin/xdg-open: 869: links2: not found
/usr/bin/xdg-open: 869: elinks: not found
/usr/bin/xdg-open: 869: links: not found
/usr/bin/xdg-open: 869: lynx: not found
/usr/bin/xdg-open: 869: w3m: not found
xdg-open: no method available for opening 'https://accounts.google.com/o/oauth2/auth?client_id=603366585132-8iag0fj49cim828jocl872jpif08dh5i.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=http%3A%2F%2Flocalhost%3A1410%2F&response_type=code&state=9CmxR1DfU7'

It would be good to do some checks if the file actually exists and is right before calling the interactive session, or not calling it at all, when user tries to authenticate via path.

PS reported also here: https://github.com/tidyverse/googlesheets4/issues/264

jennybc commented 2 years ago

It would be good to do some checks if the file actually exists and is right ...

This runs counter to the design of gargle::token_fetch(). By design, it tries a series of auth methods, all wrapped in tryCatch(). Errors are to be expected and it's designed to soldier on and, eventually, ends up the interactive flow of last resort.

Basically, the fact that it "just works" for so many people means that there is a higher bar for folks using it in more advanced ways, i.e. with arguments. In general, if you get puzzling results in the future, this article will help:

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

Basically, if someone wants informative errors (as you're requesting), you need to set the gargle_verbosity option.

(Yes, there are certainly ways to make token_fetch() smarter around knowing whether an argument was passed in or not, but that's not on the near-term roadmap.)