import os import sys from apiclient import discovery from oauth2client import client from oauth2client import tools def main(): config_dir = get_config_dir() if not os.path.exists(config_dir): os.makedirs(config_dir) creds_path = os.path.join(config_dir, 'creds.json') store = oauth2client.file.Storage(creds_path) creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets( get_client_secrets(), scope='https://www.googleapis.com/auth/gmail.readonly' ) flow.user_agent = 'Buch Bedford Water Bill' creds = tools.run(flow, store) http = creds.authorize(httplib2.Http()) service = discovery.build('gmail', 'v1', http=http) results = service.users().messages().list(userId='me').execute() labels = results.get('labels', []) return 0 def get_client_secrets(): return os.environ.get('CLIENT_SECRETS_JSON', 'client_secrets.json') def get_config_dir(): return os.path.expanduser(os.environ.get('config_dir', '~/.config/bbwb')) if __name__ == '__main__': sys.exit(main())