google / gmail-oauth2-tools

Tools and sample code for authenticating to Gmail with OAuth2
Apache License 2.0
405 stars 211 forks source link

add save_token feature #71

Open bcarrella opened 6 months ago

bcarrella commented 6 months ago

Implemented new feature to allow saving of tokens to current working directory for later use (e.g. use in script).

junyer commented 6 months ago

Thanks for the PR! Could you please explain what problem is being solved for most users of python/oauth2.py? Specifically, why might one want to specify --save_token (note that there's no corresponding --load_token) and, moreover, why might one need to save both tokens?

bcarrella commented 6 months ago

Hi, in my use case, I need to develop shell script to schedule email fetching. I need to persist token info between different runs avoiding to refresh token until expires. I would like to delegate all the oauth2 stuff to this script. I need to save both tokens because the initial_token.json will store refresh_token data, the refreshed_token.js will store the actual acces_token to use for IMAP authentication. I hope my intention is more clear know. Maybe a --load_token feature can be useful, but is not my priority at the moment.

junyer commented 6 months ago

Thanks for the explanation! In that case, I'm not sure why the existing --quiet option isn't sufficient? When a user specifies --generate_oauth2_token, they would visit the URL in their browser (i.e. this step is necessarily interactive) and presumably then persist the refresh token manually. The subsequent steps should already be able to be wrapped with, say, a shell script. (I believe this is why only --refresh_token and --generate_oauth2_string behave differently when --quiet is specified.)