【问题标题】:Using the gmail python API how can I get the most recent email that does not have a label "read"使用 gmail python API 如何获取没有标签“已读”的最新电子邮件
【发布时间】:2021-04-06 01:33:12
【问题描述】:
results = service.users().messages().list(userId='me', labelIds=['Label_8507504117657095973'], maxResults=1).execute()

此 sn-p 获取具有任意标签“已读”的最新电子邮件。我如何调整它以获取没有具有此标签的最新电子邮件?

results = service.users().messages().list(userId='me', labelIds!=['Label_8507504117657095973'], maxResults=1).execute()

= 替换为!= 由于某种原因返回错误不起作用:

results = service.users().messages().list(userId='me', labelIds!=['Label_8507504117657095973''], maxResults=1).execute() SyntaxError: positional argument follows keyword argument

【问题讨论】:

    标签: python python-3.x google-api gmail-api google-api-python-client


    【解决方案1】:

    答案:

    您可以使用 Gmail 搜索运算符进行查询,而不是使用 labelIds 参数。

    更多信息:

    根据Gmail search operators 上的文档:

    您可以使用称为搜索运算符的字词或符号来过滤您的 Gmail 搜索结果。您还可以组合运算符来进一步过滤您的结果。

    What you can search by Search operator & example
    Messages that have a certain label label:
    Example: label:friends

    因此,您可以在q 参数中使用上述搜索词来删除没有的结果,而不是在labelIds 参数中使用标签ID Label_8507504117657095973标签:

    labelName = "yourLabelName"
    results = service.users()
                     .messages()
                     .list(userId='me', q="-label:"+labelName, maxResults=1)
                     .execute() 
    

    这将返回一个没有给定标签的结果:

    {
      "messages": [
        {
          "id": "XXXXXXXXXXXXXXXX",
          "threadId": "YYYYYYYYYYYYYYYY"
        }
      ],
      "nextPageToken": "1309905752354234521",
      "resultSizeEstimate": 1
    }
    

    参考资料:

    【讨论】:

    • 解码后只会打印多次“maxResults=1”而不是电子邮件。
    • 我不确定我理解你的意思,你能解释一下吗?
    • 它打印这个:b'maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1maxResults=1 但更长
    • 嗯,我不知道你为什么会得到这个结果。您能否编辑您的问题以包括您的完整通话和打印行?
    • @TheTroupeMaster 你能解释一下未来可能遇到同样问题的其他用户的问题吗?
    猜你喜欢
    • 2018-11-03
    • 2016-07-30
    • 2021-08-14
    • 1970-01-01
    • 2018-11-02
    • 2021-02-14
    • 2019-09-02
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多