Gmail मैसेज की सूची

इस पेज पर, Gmail API के users.messages.list तरीके को कॉल करने का तरीका बताया गया है.

यह तरीका, Gmail के Message संसाधनों का एक कलेक्शन दिखाता है. इसमें मैसेज id और threadId शामिल होते हैं. मैसेज की पूरी जानकारी पाने के लिए, users.messages.get तरीके का इस्तेमाल करें.

ज़रूरी शर्तें

Python

Gmail API चालू किया गया हो. इसके लिए, Gmail API Python क्विकस्टार्ट पूरा करें.

मैसेज की सूची बनाना

users.messages.list तरीके में, मैसेज फ़िल्टर करने के लिए कई क्वेरी पैरामीटर इस्तेमाल किए जा सकते हैं:

  • maxResults: जवाब के तौर पर ज़्यादा से ज़्यादा कितने मैसेज दिखाए जाएं. डिफ़ॉल्ट रूप से, यह संख्या 100 होती है. हालांकि, इसे ज़्यादा से ज़्यादा 500 तक बढ़ाया जा सकता है.
  • pageToken: नतीजों के किसी पेज को वापस पाने के लिए टोकन.
  • q: मैसेज फ़िल्टर करने के लिए क्वेरी स्ट्रिंग, जैसे कि from:someuser@example.com is:unread".
  • labelIds: सिर्फ़ उन मैसेज को वापस लाएं जिनके लेबल, बताए गए सभी लेबल आईडी से मेल खाते हों.
  • includeSpamTrash: नतीजों में SPAM और TRASH से मिले मैसेज शामिल करें.

कोड सैंपल

Python

यहां दिए गए कोड सैंपल में, पुष्टि किए गए Gmail उपयोगकर्ता के लिए मैसेज की सूची बनाने का तरीका बताया गया है. यह कोड, पेज नंबर के हिसाब से नतीजे दिखाने की सुविधा को मैनेज करता है, ताकि क्वेरी से मेल खाने वाले सभी मैसेज वापस पाए जा सकें.

gmail/snippet/list_messages.py
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 messages.
    """
    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)
        results = (
            service.users().messages().list(userId="me", labelIds=["INBOX"]).execute()
        )
        messages = results.get("messages", [])

        if not messages:
            print("No messages found.")
            return

        print("Messages:")
        for message in messages:
            print(f'Message ID: {message["id"]}')
            msg = (
                service.users().messages().get(userId="me", id=message["id"]).execute()
            )
            print(f'  Subject: {msg["snippet"]}')

    except HttpError as error:
        # TODO(developer) - Handle errors from gmail API.
        print(f"An error occurred: {error}")


if __name__ == "__main__":
    main()

users.messages.list तरीके से मिले जवाब के मुख्य हिस्से में यह जानकारी होती है:

  • messages[]: यह Message संसाधनों का कलेक्शन होता है.
  • nextPageToken: यह टोकन, नतीजों के कई पेजों वाले अनुरोधों के लिए होता है. इसका इस्तेमाल, ज़्यादा मैसेज दिखाने के लिए बाद में किए जाने वाले कॉल के साथ किया जा सकता है.
  • resultSizeEstimate: नतीजों की कुल अनुमानित संख्या.

पूरे मैसेज का कॉन्टेंट और मेटाडेटा फ़ेच करने के लिए, users.messages.get तरीके को कॉल करने के लिए, message.id फ़ील्ड का इस्तेमाल करें.