aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--email_helpers.py28
1 files changed, 13 insertions, 15 deletions
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: