aboutsummaryrefslogtreecommitdiffstats
path: root/python/test_oauth.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/test_oauth.py')
-rw-r--r--python/test_oauth.py108
1 files changed, 0 insertions, 108 deletions
diff --git a/python/test_oauth.py b/python/test_oauth.py
deleted file mode 100644
index 41c1931a..00000000
--- a/python/test_oauth.py
+++ /dev/null
@@ -1,108 +0,0 @@
-from datetime import datetime
-import os
-
-from apiclient.discovery import build
-from apiclient import errors
-from httplib2 import Http
-import oauth2client
-from oauth2client import client
-from oauth2client import tools
-import json
-import base64
-
-import argparse
-flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
-
-SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
-CLIENT_SECRET_FILE = 'secret.json'
-APPLICATION_NAME = 'Swaptions'
-
-
-def get_credentials():
- """Gets valid user credentials from storage.
-
- If nothing has been stored, or if the stored credentials are invalid,
- the OAuth2 flow is completed to obtain the new credentials.
-
- Returns:
- Credentials, the obtained credential.
- """
- home_dir = os.path.expanduser('~')
- credential_dir = os.path.join(home_dir, '.credentials')
- if not os.path.exists(credential_dir):
- os.makedirs(credential_dir)
- credential_path = os.path.join(credential_dir,
- 'gmail-quickstart.json')
-
- store = oauth2client.file.Storage(credential_path)
- credentials = store.get()
- if not credentials or credentials.invalid:
- 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)
- return credentials
-
-def ListMessagesWithLabels(service, user_id, label_ids=[]):
- """List all Messages of the user's mailbox with label_ids applied.
-
- Args:
- service: Authorized Gmail API service instance.
- user_id: User's email address. The special value "me"
- can be used to indicate the authenticated user.
- label_ids: Only return Messages with these labelIds applied.
-
- Returns:
- List of Messages that have all required Labels applied. Note that the
- returned list contains Message IDs, you must use get with the
- appropriate id to get the details of a Message.
- """
- try:
- response = service.users().messages().list(userId=user_id,
- labelIds=label_ids).execute()
- messages = []
- if 'messages' in response:
- messages.extend(response['messages'])
- while 'nextPageToken' in response:
- page_token = response['nextPageToken']
- response = service.users().messages().list(userId=user_id,
- labelIds=label_ids,
- pageToken=page_token).execute()
- messages.extend(response['messages'])
-
- return messages
- except errors.HttpError as error:
- print(json.loads(error.content.decode('utf-8'))['error']['message'])
-def getListLabels(service, user_id):
- try:
- response = service.users().labels().list(userId=user_id).execute()
- labels = response['labels']
- return {label['name']: label['id'] for label in labels}
- except errors.HttpError as error:
- print(json.loads(error.content.decode('utf-8'))['error']['message'])
-
-def get_msg(service, user_id, msg_id):
- try:
- message = service.users().messages().get(userId=user_id, id=msg_id, format='full').execute()
- return message
- except errors.HttpError as error:
- print(json.loads(error.content.decode('utf-8'))['error']['message'])
-
-def msg_content(msg):
- return base64.b64decode(msg['payload']['body']['data']).decode('utf-8')
-
-def main():
- """Shows basic usage of the Gmail API.
-
- Creates a Gmail API service object and outputs a list of label names
- of the user's Gmail account.
- """
- credentials = get_credentials()
- service = build('gmail', 'v1', http=credentials.authorize(Http()))
- labelsdict = getListLabels(service, 'me')
- r = []
- for msg in ListMessagesWithLabels(service, 'me', labelsdict['swaptions']):
- print(msg_content(get_msg(service, 'me', msg['id'])))
-
-if __name__ == '__main__':
- messages = main()