14 pass |
14 pass |
15 |
15 |
16 import os |
16 import os |
17 import sys |
17 import sys |
18 import base64 |
18 import base64 |
19 |
19 import pickle |
20 import httplib2 |
|
21 |
|
22 import oauth2client.file |
|
23 from oauth2client import client, tools |
|
24 |
20 |
25 from googleapiclient import discovery |
21 from googleapiclient import discovery |
|
22 from google_auth_oauthlib.flow import InstalledAppFlow |
|
23 from google.auth.transport.requests import Request |
26 |
24 |
27 from PyQt5.QtCore import QCoreApplication |
25 from PyQt5.QtCore import QCoreApplication |
28 |
26 |
29 import Globals |
27 import Globals |
30 |
28 |
31 |
29 |
32 SCOPES = 'https://www.googleapis.com/auth/gmail.send' |
30 SCOPES = 'https://www.googleapis.com/auth/gmail.send' |
33 CLIENT_SECRET_FILE = 'eric_client_secret.json' |
31 CLIENT_SECRET_FILE = 'eric_client_secret.json' |
|
32 CREDENTIALS_FILE = 'eric-python-email-send.pickle' |
34 APPLICATION_NAME = 'Eric Python Send Email' |
33 APPLICATION_NAME = 'Eric Python Send Email' |
35 |
34 |
36 |
35 |
37 def isCredentialsFileAvailable(): |
36 def isClientSecretFileAvailable(): |
38 """ |
37 """ |
39 Module function to check, if the credentials file has been installed. |
38 Module function to check, if the client secret file has been installed. |
40 |
39 |
41 @return flag indicating, that the credentials file is there |
40 @return flag indicating, that the credentials file is there |
42 @rtype bool |
41 @rtype bool |
43 """ |
42 """ |
44 return os.path.exists( |
43 return os.path.exists( |
50 Module function to get the Google credentials. |
49 Module function to get the Google credentials. |
51 |
50 |
52 @return Google Mail credentials |
51 @return Google Mail credentials |
53 """ |
52 """ |
54 homeDir = os.path.expanduser('~') |
53 homeDir = os.path.expanduser('~') |
55 credentialDir = os.path.join(homeDir, '.credentials') |
54 credentialsDir = os.path.join(homeDir, '.credentials') |
56 if not os.path.exists(credentialDir): |
55 if not os.path.exists(credentialsDir): |
57 os.makedirs(credentialDir) |
56 os.makedirs(credentialsDir) |
58 credentialPath = os.path.join(credentialDir, |
57 credentialsPath = os.path.join(credentialsDir, CREDENTIALS_FILE) |
59 'eric-python-email-send.json') |
58 |
60 store = oauth2client.file.Storage(credentialPath) |
59 credentials = None |
61 credentials = store.get() |
60 # The file eric-python-email-send.pickle stores the user's access and |
62 if not credentials or credentials.invalid: |
61 # refresh tokens, and is created automatically when the authorization |
63 flow = client.flow_from_clientsecrets( |
62 # flow completes for the first time. |
64 os.path.join(Globals.getConfigDir(), CLIENT_SECRET_FILE), |
63 if os.path.exists(credentialsPath): |
65 SCOPES) |
64 with open(credentialsPath, 'rb') as token: |
66 flow.user_agent = APPLICATION_NAME |
65 credentials = pickle.load(token) |
67 credentials = tools.run_flow(flow, store) |
66 # If there are no (valid) credentials available, let the user log in. |
|
67 if not credentials or not credentials.valid: |
|
68 if credentials and credentials.expired and credentials.refresh_token: |
|
69 credentials.refresh(Request()) |
|
70 else: |
|
71 flow = InstalledAppFlow.from_client_secrets_file( |
|
72 os.path.join(Globals.getConfigDir(), CLIENT_SECRET_FILE), |
|
73 SCOPES) |
|
74 credentials = flow.run_local_server() |
|
75 # Save the credentials for the next run |
|
76 with open(credentialsPath, 'wb') as credentialsFile: |
|
77 pickle.dump(credentials, credentialsFile) |
68 return credentials |
78 return credentials |
69 |
79 |
70 |
80 |
71 def GoogleMailSendMessage(message): |
81 def GoogleMailSendMessage(message): |
72 """ |
82 """ |
85 "The credentials file is not present. Has the Gmail API" |
95 "The credentials file is not present. Has the Gmail API" |
86 " been enabled?") |
96 " been enabled?") |
87 |
97 |
88 try: |
98 try: |
89 credentials = getCredentials() |
99 credentials = getCredentials() |
90 http = credentials.authorize(httplib2.Http()) |
100 service = discovery.build('gmail', 'v1', credentials=credentials) |
91 service = discovery.build('gmail', 'v1', http=http) |
|
92 if sys.version_info[0] == 2: |
101 if sys.version_info[0] == 2: |
93 message1 = _prepareMessage_v2(message) |
102 message1 = _prepareMessage_v2(message) |
94 else: |
103 else: |
95 message1 = _prepareMessage_v3(message) |
104 message1 = _prepareMessage_v3(message) |
96 result = service.users().messages()\ |
105 result = service.users().messages()\ |