从文档中我们可以知道 Users.messages 会给我们带有以下属性的消息
{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"internalDate": long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
从上面的属性我们可以使用payload.headers[] 知道发件人邮件。
payload.headers[] 将包含格式为 {"name":string , "value":string} 的字典数组。要获取发件人的邮件,我们必须找到带有name == 'From' 的字典,其对应的value 将给我们发件人的邮件
我附上我的python代码供参考
#Call the Gmail API
#Before calling the API you have to create service using User credentials
results = service.users().messages().list(userId='me',labelIds = ['INBOX']).execute()
msgs = results.get('messages',[])
for msg in msgs:
mg = service.users().messages().get(userId='me', id=msg['id']).execute()
msg_headers = mg['payload']['headers']
for k in range(len(msg_headers)):
if msg_headers[k]['name'] == 'From':
print(msg_headers[k]['value'])
print("\n")