【发布时间】:2020-04-24 11:52:11
【问题描述】:
在 Python Gmail API 文档中,get 函数提供 json 输出,我可以访问“sn-p”数据。我想访问“标题”键的“值”信息。我觉得这应该只是获取多级 JSON 数据,但它正在逃避我。有人可以帮帮我吗?
http://googleapis.github.io/google-api-python-client/docs/dyn/gmail_v1.users.messages.html#get
{ # An email message.
**"headers": [ # List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.**
{
"name": "A String", # The name of the header before the : separator. For example, To.
**"value": "A String", # The value of the header after the : separator. For example, someuser@example.com.**
},
],
"snippet": "A String", # A short part of the message text.
"raw": "A String", # The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied.
"sizeEstimate": 42, # Estimated size in bytes of the message.
"threadId": "A String", # The ID of the thread the message belongs to. To add a message or draft to a thread, the following criteria must be met:
# - The requested threadId must be specified on the Message or Draft.Message you supply with your request.
# - The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard.
# - The Subject headers must match.
"labelIds": [ # List of IDs of labels applied to this message.
"A String",
],
"id": "A String", # The immutable ID of the message.
}
for message in messages:
msg = service.users().messages().get(userId='me', id=message['id']).execute()
print(msg['snippet']) # This works
print(msg['value']) # This doesn't and I tried ['headers']['value'] also.
print("\n")
time.sleep(1)
Traceback (most recent call last):
File "C:/Users/Notebook/PycharmProjects/Jarvis/Gmail.py", line 65, in <module>
main()
File "C:/Users/Notebook/PycharmProjects/Jarvis/Gmail.py", line 58, in main
print("New Message: " + msg['value'])
KeyError: 'value'
【问题讨论】: