对于某些消息,我相信没有“部分”组件。我认为下面的代码涵盖了大多数消息,但是在尝试获取某些消息时它仍然抛出一个 keyerror: 'data'。我查看了这些消息的 json 结果,它们都包含“数据”,不确定发生了什么。Traceback 显示 keyerror 来自包含“parts”的消息
追溯
Traceback (most recent call last):
File "gmail.py", line 168, in <module>
final_message_list.append(GetMessage(gmail_service, 'me', message_id))
File "gmail.py", line 150, in GetMessage
message_raw = message['payload']['parts'][0]['body']['data']
(在 GetMessage 内)
# Pull Raw Message Body from Response, some emails may not contain 'parts'
if 'parts' in message['payload']:
message_raw = message['payload']['parts'][0]['body']['data']
else:
message_raw = message['payload']['body']['data']