aboutsummaryrefslogtreecommitdiffstats
path: root/email_helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'email_helpers.py')
-rw-r--r--email_helpers.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/email_helpers.py b/email_helpers.py
index 487bb7e..de19fe8 100644
--- a/email_helpers.py
+++ b/email_helpers.py
@@ -1,7 +1,6 @@
from apiclient.discovery import build
from apiclient import errors
from google.oauth2.credentials import Credentials
-#from oauth2client import client, tools
from google_auth_oauthlib.flow import InstalledAppFlow
import os
@@ -30,12 +29,17 @@ def get_gmail_service():
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'news.horel@gmail.com')
- credentials = Credentials.from_authorized_user_file(credential_path)
- if False:
- #flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
- #flow = Flow.from_client_secrets_file(CLIENT_SECRET_FILE, scopes=SCOPES)
+ try:
+ credentials = Credentials.from_authorized_user_file(credential_path)
+ except:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
credentials = flow.run_console()
+ to_save = {}
+ for attr in["token", "refresh_token", "id_token", "token_uri",
+ "client_id", "client_secret", "scopes"]:
+ to_save[attr] = getattr(credentials, attr)
+ with open(credential_path, "w") as fh:
+ json.dump(to_save, fh)
service = build('gmail', 'v1', credentials=credentials)
return service