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

इस दस्तावेज़ में, Gmail API के messages.list तरीके को कॉल करने का तरीका बताया गया है.

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

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

Python

आपके पास Gmail API की सुविधा वाला Google Cloud प्रोजेक्ट होना चाहिए. इसके लिए, Gmail API Python क्विकस्टार्ट गाइड पूरी करें.

मैसेज की सूची देखना

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()

messages.list तरीका, रिस्पॉन्स का मुख्य हिस्सा दिखाता है. इसमें ये चीज़ें शामिल होती हैं:

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

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