【问题标题】:Python Gmail API won't pass labelIds Parameter to List methodPython Gmail API 不会将 labelIds 参数传递给 List 方法
【发布时间】:2019-02-14 05:42:15
【问题描述】:

我一直在尝试设置一个 Python 脚本,该脚本涉及在 Gmail 帐户中查询未读邮件。理想情况下,我想使用 Gmail API 的“list”方法和可选的查询参数过滤标签 ID 为“UNREAD”的邮件。

当我在 Google 的网站 (https://developers.google.com/gmail/api/v1/reference/users/messages/list) 上对此进行测试时,它可以正常工作。

但在我的脚本中,labelId 参数似乎没有正确传递,我的输出始终是完整的消息列表。

这是我现在得到的代码行:

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

这会返回收件箱中的所有邮件,而不是仅过滤为 UNREAD。

我遇到过一些关于人们对可选查询(Gmail API 列表方法中的“q”参数)有类似问题但对于 labelIds 没有类似问题的文档。

有人遇到过这个问题吗?

【问题讨论】:

    标签: python gmail gmail-api


    【解决方案1】:

    这个修改怎么样?我认为您的情况有几种模式。

    模式一:

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

    例如,如果您想检索收件箱中的未读邮件,您可以使用userId='me', labelIds=['UNREAD', 'INBOX']userId='me', labelIds=['UNREAD'], q='in:inbox'

    模式2:

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

    例如,如果你想找回收件箱中的未读邮件,你可以使用userId='me', q='in:inbox is:unread'

    参考资料:

    如果我误解了你的问题,我很抱歉。

    【讨论】:

    • 感谢您的回复,但这些都没有解决我的问题。收件箱中所有消息的 id 仍会返回,而不仅仅是未读。我做了更多的挖掘,它似乎与此处描述的问题相似:github.com/google/google-api-nodejs-client/issues/469 但我不确定如何应用他们的“qs”解决方法。
    • @JEK 对于给您带来的不便,我深表歉意。在我的环境中,我可以确认这些模式有效。所以我担心我的环境可能与你的不同。例如,为了检索收件箱中的消息,当使用service.users().messages().list(userId='me', labelIds=['INBOX']).execute()service.users().messages().list(userId='me', q='in:inbox').execute() 时,你会得到什么值?在我的环境中,可以从两个样本中检索收件箱中的消息。
    猜你喜欢
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多