如何使用 gmail-api 获取社交网站电子邮件的日期和时间?

How to get date and time for Social networking sites emails using gmail-api?

我一直在尝试提取来自特定用户的第一条消息。在这种情况下,来自 Facebook 的第一条消息。 数据提取正常,但日期和时间未返回 尝试使用正常的消息它正在工作。

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import time
from datetime import date

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

def main():

    store = file.Storage('token.json')
    creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))

# Call the Gmail API to fetch INBOX

results = service.users().messages().list(userId='me', q='from:security@facebookmail.com',labelIds = ['INBOX']).execute()
messages = results.get('messages', [])


if not messages:
    print ("No messages found.")
else:
    print ("Message snippets:")

for message in messages:

    msg = service.users().messages().get(userId='me', id=messages[-1]['id']).execute()
    print (msg['snippet'])



if __name__ == '__main__':
    main()

这是 facebook 的第一条消息:(没有返回日期和时间)

嘿 Shashi Deep,你最近注册了 Facebook。要完成您的 Facebook 注册,请确认您的帐户。需要采取的行动:确认您的 Facebook 帐户 Hey Shashi Deep,您最近

这是普通用户的第一条消息:(返回日期和时间)

非常感谢。 2018 年 12 月 12 日星期三,12:07 PM Shashi Deep

首先让我先声明我不是 Python 开发者。不过我看得懂。

msg = service.users().messages().get(userId='me', id=messages[-1]['id']).execute()
print (msg['snippet'])

来自 message.get 的响应应该看起来像这样

{
 "id": "1684c9dc1f5fe52f",
 "threadId": "1684bfd8ff98b154",
 "labelIds": [
  "CHAT"
 ],
 "snippet": "",
 "historyId": "4608454",
 "internalDate": "1547473633781",
 "payload": {
  "partId": "",
  "mimeType": "text/html",
  "filename": "",
  "headers": [
   {
    "name": "From",
    "value": "Linda Lawton \u003cxxxx@gmail.com\u003e"
   }
  ],
  "body": {
   "size": 6,
   "data": "8J-krvCfpKLwn6Su"
  }
 },
 "sizeEstimate": 100
}

通过执行 print (msg['snippet']) 你是说你只需要代码段字段。尝试做

print (msg['internalDate'])  

如果这没有帮助,请 运行 请在此页面上尝试我的 ID message.get 我希望看到 json 对您遇到问题的消息的回复。