Python Gmail API won't pass labelIds Parameter to List method

左心房为你撑大大i 提交于 2021-02-11 13:00:37

问题


I've been trying to get a Python script set up that involves querying a Gmail account for unread messages. Ideally I'd like to make use of the Gmail API's "list" method with the optional query parameter filtering for messages with the labelId of "UNREAD".

When I test this out on Google's site (https://developers.google.com/gmail/api/v1/reference/users/messages/list) , it works properly.

But within my script, the labelId parameter seems to not be passed correctly and my output is always the full list of messages.

Here's the line of code I've got right now:

results = service.users().messages().list(userId='me', labelIds='UNREAD').execute()

This returns all messages in the inbox, not filtered to UNREAD only.

I've come across some documentation on people having a similar issue with the optional queries ('q' parameter in the Gmail API list method) but not for labelIds.

Does anyone have any experience with this problem?


回答1:


How about this modification? I think that there are several patterns for your situation.

Pattern 1:

results = service.users().messages().list(userId='me', labelIds=['UNREAD']).execute()

For example, if you want to retrieve the unread messages in the inbox, you can use userId='me', labelIds=['UNREAD', 'INBOX'] and userId='me', labelIds=['UNREAD'], q='in:inbox'.

Pattern 2:

results = service.users().messages().list(userId='me', q='is:unread').execute()

For example, if you want to retrieve the unread messages in the inbox, you can use userId='me', q='in:inbox is:unread'.

References:

  • Users.messages: list
  • Search operators you can use with Gmail

If I misunderstand your question, I'm sorry.



来源:https://stackoverflow.com/questions/52246770/python-gmail-api-wont-pass-labelids-parameter-to-list-method

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