Skip to content

Instantly share code, notes, and snippets.

@samber
Created September 20, 2017 16:59
Show Gist options
  • Select an option

  • Save samber/0323f0ee863d531c491973bd0d834553 to your computer and use it in GitHub Desktop.

Select an option

Save samber/0323f0ee863d531c491973bd0d834553 to your computer and use it in GitHub Desktop.
POC gmail api (Oauth2 + Gmail api)
#!/usr/bin/env python3
import httplib2
import apiclient
from oauth2client.client import OAuth2WebServerFlow
scopes = [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.settings.basic',
'https://www.googleapis.com/auth/userinfo.profile',
]
flow = OAuth2WebServerFlow(client_id='***********',
client_secret='*************',
scope=" ".join(scopes),
redirect_uri='http://nothing-to-see.here/foo/bar')
auth_uri = flow.step1_get_authorize_url()
print("Please visit the following webpage:\n\n{}\n".format(auth_uri))
code = input("code: ")
credentials = flow.step2_exchange(code)
http = credentials.authorize(httplib2.Http())
gmail_api = apiclient.discovery.build("gmail", 'v1', http=http)
oauth2_api = apiclient.discovery.build('oauth2', 'v2', http=http)
labels = gmail_api.users().labels().list(userId='me').execute()
profile = gmail_api.users().getProfile(userId='me').execute()
settings = gmail_api.users().settings()
userinfos = oauth2_api.userinfo().get().execute()
gmail_sendAs = gmail_api.users().settings().sendAs().list(userId="me").execute()
print(labels)
print(profile)
print(settings)
print(userinfos)
print(gmail_sendAs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment