Last active
September 24, 2023 20:40
-
-
Save karkx/7a28840c5d263465d8a12d64f89a84fa to your computer and use it in GitHub Desktop.
Python google gmail api basic oauth2 authorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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