Skip to content

Instantly share code, notes, and snippets.

@Karklik
Last active September 24, 2023 20:40
Show Gist options
  • Save Karklik/7a28840c5d263465d8a12d64f89a84fa to your computer and use it in GitHub Desktop.
Save Karklik/7a28840c5d263465d8a12d64f89a84fa to your computer and use it in GitHub Desktop.
Python google gmail api basic oauth2 authorization
from googleapiclient.discovery import build
from oauth2client import file, client, tools
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
# To get credentials.json see https://developers.google.com/gmail/api/quickstart/python
def get_credentials():
storage = file.Storage('storage.json')
credentials = storage.get()
if credentials is None or credentials.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
credentials = tools.run_flow(flow, storage)
return credentials
def get_labels():
service = build('gmail', 'v1', credentials=get_credentials())
results = service.users().labels().list(userId='me').execute()
return results.get('labels', [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment