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?
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
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