From c13318ca8ab33c074a2fed126119607deea3d587 Mon Sep 17 00:00:00 2001 From: Tyler Hess Date: Wed, 10 Apr 2024 11:35:33 -0600 Subject: [PATCH] Add an init var for auth token from environment variable SIMPLEGMAIL_CREDENTIALS --- simplegmail/gmail.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/simplegmail/gmail.py b/simplegmail/gmail.py index 67aa2aa..b519d44 100644 --- a/simplegmail/gmail.py +++ b/simplegmail/gmail.py @@ -69,6 +69,7 @@ def __init__( access_type: str = 'offline', noauth_local_webserver: bool = False, _creds: Optional[client.OAuth2Credentials] = None, + use_envvars: bool = False ) -> None: self.client_secret_file = client_secret_file self.creds_file = creds_file @@ -79,6 +80,13 @@ def __init__( # completes for the first time. if _creds: self.creds = _creds + elif use_envvars: + # This requires that the environment variable + # 'SIMPLEGMAIL_CREDENTIALS' be set to an already authorized + # gmail_token.json contents. This gives the refresh token + # that can be used indefinitely until revoked. + self.creds = client.OAuth2Credentials.from_json( + os.environ.get('SIMPLEGMAIL_CREDENTIALS')) else: store = file.Storage(self.creds_file) self.creds = store.get()