E5Network/E5GoogleMail.py

changeset 6817
8fb29a716d1a
parent 6645
ad476851d7e0
child 6823
55bda7c2404d
--- a/E5Network/E5GoogleMail.py	Sun Feb 24 13:01:58 2019 +0100
+++ b/E5Network/E5GoogleMail.py	Sun Feb 24 17:55:36 2019 +0100
@@ -16,13 +16,11 @@
 import os
 import sys
 import base64
-
-import httplib2
-
-import oauth2client.file
-from oauth2client import client, tools
+import pickle
 
 from googleapiclient import discovery
+from google_auth_oauthlib.flow import InstalledAppFlow
+from google.auth.transport.requests import Request
 
 from PyQt5.QtCore import QCoreApplication
 
@@ -31,12 +29,13 @@
 
 SCOPES = 'https://www.googleapis.com/auth/gmail.send'
 CLIENT_SECRET_FILE = 'eric_client_secret.json'
+CREDENTIALS_FILE = 'eric-python-email-send.pickle'
 APPLICATION_NAME = 'Eric Python Send Email'
 
 
-def isCredentialsFileAvailable():
+def isClientSecretFileAvailable():
     """
-    Module function to check, if the credentials file has been installed.
+    Module function to check, if the client secret file has been installed.
     
     @return flag indicating, that the credentials file is there
     @rtype bool
@@ -52,19 +51,30 @@
     @return Google Mail credentials
     """
     homeDir = os.path.expanduser('~')
-    credentialDir = os.path.join(homeDir, '.credentials')
-    if not os.path.exists(credentialDir):
-        os.makedirs(credentialDir)
-    credentialPath = os.path.join(credentialDir,
-                                  'eric-python-email-send.json')
-    store = oauth2client.file.Storage(credentialPath)
-    credentials = store.get()
-    if not credentials or credentials.invalid:
-        flow = client.flow_from_clientsecrets(
-            os.path.join(Globals.getConfigDir(), CLIENT_SECRET_FILE),
-            SCOPES)
-        flow.user_agent = APPLICATION_NAME
-        credentials = tools.run_flow(flow, store)
+    credentialsDir = os.path.join(homeDir, '.credentials')
+    if not os.path.exists(credentialsDir):
+        os.makedirs(credentialsDir)
+    credentialsPath = os.path.join(credentialsDir, CREDENTIALS_FILE)
+
+    credentials = None
+    # The file eric-python-email-send.pickle stores the user's access and
+    # refresh tokens, and is created automatically when the authorization
+    # flow completes for the first time.
+    if os.path.exists(credentialsPath):
+        with open(credentialsPath, 'rb') as token:
+            credentials = pickle.load(token)
+    # If there are no (valid) credentials available, let the user log in.
+    if not credentials or not credentials.valid:
+        if credentials and credentials.expired and credentials.refresh_token:
+            credentials.refresh(Request())
+        else:
+            flow = InstalledAppFlow.from_client_secrets_file(
+                os.path.join(Globals.getConfigDir(), CLIENT_SECRET_FILE),
+                SCOPES)
+            credentials = flow.run_local_server()
+        # Save the credentials for the next run
+        with open(credentialsPath, 'wb') as credentialsFile:
+            pickle.dump(credentials, credentialsFile)
     return credentials
 
 
@@ -87,8 +97,7 @@
     
     try:
         credentials = getCredentials()
-        http = credentials.authorize(httplib2.Http())
-        service = discovery.build('gmail', 'v1', http=http)
+        service = discovery.build('gmail', 'v1', credentials=credentials)
         if sys.version_info[0] == 2:
             message1 = _prepareMessage_v2(message)
         else:

eric ide

mercurial