metadataHeaders option failing with multiple headers

孤街浪徒 提交于 2019-12-10 00:03:12

问题


Here is my code snippet:

    if messages['messages']:
        for message in messages['messages']:
            batch.add(gmail_client.users().messages().get(userId='me', id=message['id'], format='metadata', fields="payload,threadId,id", metadataHeaders="from,to,date,subject"), callback=messageCallback)

    batch.execute()

This works fine with just one option in metadataHeaders, but with multiple headers listed it's not returning any headers at all. Any ideas?


回答1:


Just figured it out. The documentation is incorrect, the correct format for this parameter is an array of strings rather than a single string. You can see the error on this page:

https://developers.google.com/gmail/api/v1/reference/users/messages/get




回答2:


You can do like following:

message2 =gmail_service.users().messages().get(userId='me', id=thread['id'], format='metadata', metadataHeaders=['subject','from','to','date']).execute()
for num in range(0,4):
  if message2['payload']['headers'][num]['name'].lower() == "subject":
    subject=message2['payload']['headers'][num]['value']
  elif  message2['payload']['headers'][num]['name'].lower() == "from": 
    From=message2['payload']['headers'][num]['value']
  elif  message2['payload']['headers'][num]['name'].lower() == "to": 
    to=message2['payload']['headers'][num]['value']
  elif  message2['payload']['headers'][num]['name'].lower() == "date": 
    date=message2['payload']['headers'][num]['value']

f.write("Date : %s "  % date.encode('utf8')+'\n')
f.write("Subject : %s "  % subject.encode('utf8')+'\n')
f.write("From : %s "  % From.encode('utf8')+'\n')
f.write("To : %s "  % to.encode('utf8')+'\n')


来源:https://stackoverflow.com/questions/27518926/metadataheaders-option-failing-with-multiple-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!