From b05e67549c0ea2d1c66b33a09f9d06aa35727702 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Sat, 29 Dec 2018 12:10:55 -0500 Subject: use more modern libraries --- email_helpers.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'email_helpers.py') diff --git a/email_helpers.py b/email_helpers.py index a3e2b29..487bb7e 100644 --- a/email_helpers.py +++ b/email_helpers.py @@ -1,19 +1,19 @@ from apiclient.discovery import build from apiclient import errors -from httplib2 import Http -import oauth2client -from oauth2client import client, tools +from google.oauth2.credentials import Credentials +#from oauth2client import client, tools +from google_auth_oauthlib.flow import InstalledAppFlow + import os import json import base64 from email.message import EmailMessage import email from email.utils import parseaddr, parsedate_to_datetime -import argparse from bs4 import BeautifulSoup from pytz import timezone -SCOPES = 'https://www.googleapis.com/auth/gmail.modify' +SCOPES = ['https://www.googleapis.com/auth/gmail.modify'] CLIENT_SECRET_FILE = os.path.expanduser('~/client_id.json') APPLICATION_NAME = 'Famille' @@ -30,15 +30,13 @@ 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') - store = oauth2client.file.Storage(credential_path) - credentials = store.get() - if not credentials or credentials.invalid: - flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() - flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) - flow.user_agent = APPLICATION_NAME - credentials = tools.run_flow(flow, store, flags) - print('Storing credentials to ' + credential_path) - service = build('gmail', 'v1', http=credentials.authorize(Http())) + 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) + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES) + credentials = flow.run_console() + service = build('gmail', 'v1', credentials=credentials) return service class GmailMessage(EmailMessage): @@ -50,7 +48,7 @@ class GmailMessage(EmailMessage): def send(self): try: message = (self._service.users().messages(). - send(userId='me',body=self.msgdict()) + send(userId='me', body=self.msgdict()) .execute()) print('Message Id: %s' % message['id']) except errors.HttpError as error: -- cgit v1.2.3-70-g09d2