问题
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