Part 1 - Api Docs and scopes
01
Go to the Gmail api homepage, and click on Guides
https://developers.google.com/gmail/api/guides
Here, the methods, classes, and use cases are described

02
Scopes
Go to,
Authentification & authorization
→ Choose scopes
https://developers.google.com/gmail/api/auth/scopes

03
Common scopes are,
https://www.googleapis.com/auth/gmail.labels
and,
https://www.googleapis.com/auth/gmail.settings.basic

04
https://mail.google.com/
Gives you full permission to gmail

Part 2 - Documentation Python for Gmail API
01
Go to Guides
→Gmail API →Quickstarts → Python
then,
https://developers.google.com/gmail/api/quickstart/python

02
Instructions for setup and install
You can follow the instructions on this page to see up, or continue to Part 3 - Create Project
Part 3 - google cloud platform - Create Project
console.cloud.google
01
Go to google cloud platform,
https://console.cloud.google.com/welcome?authuser=0&organizationId=0

02
Click the Project drop menu and then click NEW PROJECT

03
Name and Create

Part 4 - Manage Gmail API - API Library
01
In the project, go to APIs & Services
→ Library
https://console.cloud.google.com/apis/library?authuser=0&project=gmail-api-jiejenn-

02
Search for Gmail Api

03
Click Gmail Api

04
Click Enable
or MANAGE

Part 5 - Credentials - Api key & Oauth Client_Secret
01
Go to Credentials
page
https://console.cloud.google.com/apis/credentials?authuser=0&project=gmail-api-jiejenn-408314

02
Click CREATE CREDENTIALS
and select
OAuth Client ID
Review Varisty tutors Page Vasirty Tutors Session 1(link not working yet)

03
Click,
CONFIGURE CONSENT SCREEN

04
Choose External
You can not use Internal
anymore, promt saying
Because you're not a Google Workspace user, you can only make your app avaible to external (general audience) users

05
Name and enter an email,
06
Enter the full permission scope
https://mail.google.com/

07
(** Not working) trying to add email here but says it is an ineligble account,
Click SAVE AND CONTINUE

08
Add your email as a Test User
and then hit SAVE AND CONTINUE
Rememeber add
tstudystuff@gmail too
Never mind, doens't work just do every in this lesson for tstudystuff

Part 6 - Create OAuth Client Id - Client_Secret
01
Go to Credentials then, OAuth Client Id

02
Chooose Desktop App
then hit Save and Continue

03
Download the json file to the same directory as the project

Part 7 - Create a VENV & Install Libraries
01
Go to Corey Schafer 01-flask for review, and install a venv in the directory of the project, and install the libraries
python -m venv venv
./venv/bin/activate

02
If ./venv/bin/activate
Does Not Work
run
source ./venv/bin/activate

03
I can't figure out how to deactivate the venv
source venv/bin/deactivate
04
Go to https://developers.google.com/gmail/api/quickstart/python , and install the client libraries
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Part 8 - Python Script
01
Go to https://learndataanalysis.org/google-py-file-source-code/ and make sure you install,
pip install –upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Into your venv

02
Place the script from above into
script named google_api.py
that is in the same directory as your main script script.py
and venv
Import the function create_Service
into your main script


0
do the following changes, the
print(dir(service))
Will print all the methods an classes in the api
import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"] def main(): creds = None if os.path.exists("token.json"): creds = Credentials.from_authorized_user_file("token.json", SCOPES) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( "client_secret.json", SCOPES ) creds = flow.run_local_server(port=0) with open("token.json", "w") as token: token.write(creds.to_json()) try: service = build("gmail", "v1", credentials=creds) print(dir(service)) except HttpError as error: print(f"An error occurred: {error}") if __name__ == "__main__": main()

0a
We can do the same and see the methods for results
which is service.users().message().list(userId='me').execute()
import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"] def main(): creds = None if os.path.exists("token.json"): creds = Credentials.from_authorized_user_file("token.json", SCOPES) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( "client_secret.json", SCOPES ) creds = flow.run_local_server(port=0) with open("token.json", "w") as token: token.write(creds.to_json()) try: service = build("gmail", "v1", credentials=creds) print(dir(service)) results = service.users().messages().list(userId="me").execute() print(dir(results)) except HttpError as error: print(f"An error occurred: {error}") if __name__ == "__main__": main()

03
Place the variables into the create_service
function with the
scope
https://mail.google.com/
scopes can be founed at
https://developers.google.com/gmail/api/auth/scopes
0b
Use the
get()
function
you can see from dir(results)
to get the
from messages
results
messages = results.get('messages',[])
You can see the ids for each message
