【问题标题】:How to write Python to get all Gmail message ID's?如何编写 Python 来获取所有 Gmail 邮件 ID?
【发布时间】:2021-08-26 00:45:41
【问题描述】:

我想列出来自使用 Gmail API 的 Gmail 帐户的所有邮件 ID。到目前为止,我已经能够列出消息 ID 的第一页和第二页。我知道我必须使用 pageToken 才能进入下一页结果,但我不知道如何重组我的代码,所以我没有使用 1、2、3 等变量来调用每个页面。源代码如下。

get_email_ids.py:

from __future__ import print_function
import os.path
from collections import Counter
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# 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.
    """
    creds = None
    user_id = "me"
    # 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())

    service = build('gmail', 'v1', credentials=creds)

    ### Call the Gmail API

    ### Show messages

    token = ''
    messages = service.users().messages().list(userId=user_id,pageToken=token).execute().get('messages', [])
    token = service.users().messages().list(userId=user_id,pageToken=token).execute().get('nextPageToken', [])
    print(messages,token)

    messages2 = service.users().messages().list(userId=user_id,pageToken=token).execute().get('messages', [])
    token2 = service.users().messages().list(userId=user_id,pageToken=token).execute().get('nextPageToken', [])
    print(messages2,token2) 


if __name__ == '__main__':
    main()

get_email_ids.py 的结果(缩短):

[{'id': '179ed5ae720de1f6', 'threadId': '179ed5ae720de1f6'}, ... {'id': '179ba226644a079a', 'threadId': '17972318184138fa'}] 09573475999783117733
[{'id': '179b9f8852d3b09d', 'threadId': '179b9f8852d3b09d'}, ... {'id': '1797fa390caa3454', 'threadId': '1797fa390caa3454'}] 07601624978802434502

【问题讨论】:

  • 使用不带 1、2、3 的相同变量。结果附加到同一个列表 - 即。 all_message += messages。并在for-loop 中运行代码。或while True 循环。

标签: python gmail-api


【解决方案1】:

我无法对其进行测试,但我会使用相同的变量 messages、token 而不使用 1,2,3,结果我会将所有消息添加到同一个列表中。我会在某个循环中运行它。

类似的东西

all_messages = []

token = ''

while True:
    messages = service.users().messages().list(userId=user_id, pageToken=token).execute().get('messages', [])
    token = service.users().messages().list(userId=user_id, pageToken=token).execute().get('nextPageToken', [])
    print(messages, token)

    if not messages:
        break
    
    #all_messages.extend(messages)  # `extend` or `+=`, not `append`
    all_messages += messages        # `extend` or `+=`, not `append`
    

我只是不知道 API 是如何通知没有更多消息的 - 可能它返回空列表,或者它给出空令牌,或者它可能引发错误。


编辑:

其他用户的信息:正如评论中提到的@emmalynnh

When there are no more messages it gives an empty token 
and the API will return a 400 error if you try to request.

【讨论】:

  • 谢谢@furas,这对我有用。当没有更多消息时,它会给出一个空令牌,如果您尝试请求,API 将返回 400 错误。
【解决方案2】:

可以制作@furas 的改进版本。

all_messages = []
token = ''

while True:
    service_messages = service.users().messages()
    messages = service_messages.list(userId=user_id, pageToken=token).execute().get('messages', [])
    token = service_messages.list(userId=user_id, pageToken=token).execute().get('nextPageToken', [])
    if not messages:
        break
    all_messages += messages

print(all_messages)    

【讨论】:

  • 我认为你甚至可以在 while True 之前创建 service_messages - 但我无法测试它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多