Gmail API:如何获取邮件正文?
Gmail API: How can I get the message body?
根据下面引用的文档,消息应包含 MessagePart,而 MessagePart 又应包含 MessagePartBody。
https://developers.google.com/gmail/api/reference/rest/v1/users.messages#Message
当我 运行 下面的代码时(它只是找到 here 的示例脚本的修改版本,其中消息替换了标签)
from __future__ import print_function
import pickle
import os.path
import openpyxl
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://mail.google.com/']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle 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.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API
results = service.users().messages().list(userId='me').execute()
messages = results.get('messages', [])
if not messages:
print('No messages found.')
else:
print('Messages:')
for message in messages:
print(message)
if __name__ == '__main__':
main()
我只得到 Messageids 和 Threadsids 例如:
Messages:
{'id': '177045ba844e1991', 'threadId': '177045ba844e1991'}
{'id': '1770415ccdd222d7', 'threadId': '1770415ccdd222d7'}
{'id': '17703970573550eb', 'threadId': '17703970573550eb'}
{'id': '177031073928a223', 'threadId': '177031073928a223'}
{'id': '17702de505951773', 'threadId': '17702de505951773'}
{'id': '17702a3e6d1893de', 'threadId': '17702a3e6d1893de'}
如何使用此 API 获取消息的实际正文?
As stated in the documentation of users.messages.list
:
Note that each message resource contains only an id
and a threadId
. Additional message details can be fetched using the messages.get method.
所以基本上它是一个两步过程:
- 您使用
list
获取收件箱中的电子邮件。
- 使用
get
阅读有关它们的信息。
它看起来像:
results = service.users().messages().list(userId='me').execute()
messages = results.get('messages', [])
messages = [service.users().messages().get(userId='me', id=msg['id']).execute() for msg in messages]
现在,如果您这样做,您将 运行 遇到问题,因为这会使请求 1 对 1。通过一个请求获取多条消息的方法是使用批处理请求:
results = service.users().messages().list(userId='me').execute()
message_ids = results.get('messages', [])
messages = []
def add(id, msg, err):
# id is given because this will not be called in the same order
if err:
print(err)
else:
messages.append(msg)
batch = service.new_batch_http_request()
for msg in message_ids:
batch.add(service.users().messages().get(userId='me', id=msg['id']), add)
batch.execute()
关于批处理请求的一个重要注意事项是调用回调的顺序可能与您开始时的顺序不同。
参考资料
根据下面引用的文档,消息应包含 MessagePart,而 MessagePart 又应包含 MessagePartBody。
https://developers.google.com/gmail/api/reference/rest/v1/users.messages#Message
当我 运行 下面的代码时(它只是找到 here 的示例脚本的修改版本,其中消息替换了标签)
from __future__ import print_function
import pickle
import os.path
import openpyxl
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://mail.google.com/']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle 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.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API
results = service.users().messages().list(userId='me').execute()
messages = results.get('messages', [])
if not messages:
print('No messages found.')
else:
print('Messages:')
for message in messages:
print(message)
if __name__ == '__main__':
main()
我只得到 Messageids 和 Threadsids 例如:
Messages:
{'id': '177045ba844e1991', 'threadId': '177045ba844e1991'}
{'id': '1770415ccdd222d7', 'threadId': '1770415ccdd222d7'}
{'id': '17703970573550eb', 'threadId': '17703970573550eb'}
{'id': '177031073928a223', 'threadId': '177031073928a223'}
{'id': '17702de505951773', 'threadId': '17702de505951773'}
{'id': '17702a3e6d1893de', 'threadId': '17702a3e6d1893de'}
如何使用此 API 获取消息的实际正文?
As stated in the documentation of users.messages.list
:
Note that each message resource contains only an
id
and athreadId
. Additional message details can be fetched using the messages.get method.
所以基本上它是一个两步过程:
- 您使用
list
获取收件箱中的电子邮件。 - 使用
get
阅读有关它们的信息。
它看起来像:
results = service.users().messages().list(userId='me').execute()
messages = results.get('messages', [])
messages = [service.users().messages().get(userId='me', id=msg['id']).execute() for msg in messages]
现在,如果您这样做,您将 运行 遇到问题,因为这会使请求 1 对 1。通过一个请求获取多条消息的方法是使用批处理请求:
results = service.users().messages().list(userId='me').execute()
message_ids = results.get('messages', [])
messages = []
def add(id, msg, err):
# id is given because this will not be called in the same order
if err:
print(err)
else:
messages.append(msg)
batch = service.new_batch_http_request()
for msg in message_ids:
batch.add(service.users().messages().get(userId='me', id=msg['id']), add)
batch.execute()
关于批处理请求的一个重要注意事项是调用回调的顺序可能与您开始时的顺序不同。