I just started learning about API’s a few days ago and the current project I’m working on has me diving in the deep end using the Gmail API.
I’ve followed the quickstart here:
https://developers.google.com/gmail/api/quickstart/python
And managed to get an email’s contents using this:
https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get
However, most of the ‘data’ from the email seems to be encoded.
I want to get a link from an email and then navigate to that link. How can I extract a link from an email using the Gmail API?
Here’s what my program currently looks like
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
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
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(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
# Call the Gmail API
service = build("gmail", "v1", credentials=creds)
message = service.users().messages().get(userId="me", id="abcdefg123456").execute()
print(message)
happyMaskSalesman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.