|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START gmail_list_messages] |
| 16 | +import os.path |
| 17 | +from google.auth.transport.requests import Request |
| 18 | +from google.oauth2.credentials import Credentials |
| 19 | +from google_auth_oauthlib.flow import InstalledAppFlow |
| 20 | +from googleapiclient.discovery import build |
| 21 | +from googleapiclient.errors import HttpError |
| 22 | + |
| 23 | +# If modifying these scopes, delete the file token.json. |
| 24 | +SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"] |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + """Shows basic usage of the Gmail API. |
| 29 | + Lists the user's Gmail messages. |
| 30 | + """ |
| 31 | + creds = None |
| 32 | + # The file token.json stores the user's access and refresh tokens, and is |
| 33 | + # created automatically when the authorization flow completes for the first |
| 34 | + # time. |
| 35 | + if os.path.exists("token.json"): |
| 36 | + creds = Credentials.from_authorized_user_file("token.json", SCOPES) |
| 37 | + # If there are no (valid) credentials available, let the user log in. |
| 38 | + if not creds or not creds.valid: |
| 39 | + if creds and creds.expired and creds.refresh_token: |
| 40 | + creds.refresh(Request()) |
| 41 | + else: |
| 42 | + flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES) |
| 43 | + creds = flow.run_local_server(port=0) |
| 44 | + # Save the credentials for the next run |
| 45 | + with open("token.json", "w") as token: |
| 46 | + token.write(creds.to_json()) |
| 47 | + |
| 48 | + try: |
| 49 | + # Call the Gmail API |
| 50 | + service = build("gmail", "v1", credentials=creds) |
| 51 | + results = ( |
| 52 | + service.users().messages().list(userId="me", labelIds=["INBOX"]).execute() |
| 53 | + ) |
| 54 | + messages = results.get("messages", []) |
| 55 | + |
| 56 | + if not messages: |
| 57 | + print("No messages found.") |
| 58 | + return |
| 59 | + |
| 60 | + print("Messages:") |
| 61 | + for message in messages: |
| 62 | + print(f'Message ID: {message["id"]}') |
| 63 | + msg = ( |
| 64 | + service.users().messages().get(userId="me", id=message["id"]).execute() |
| 65 | + ) |
| 66 | + print(f' Subject: {msg["snippet"]}') |
| 67 | + |
| 68 | + except HttpError as error: |
| 69 | + # TODO(developer) - Handle errors from gmail API. |
| 70 | + print(f"An error occurred: {error}") |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
| 75 | + |
| 76 | +# [END gmail_list_messages] |
0 commit comments