【问题标题】:Gmail API - Is there a way to list messages in sorted order w.r.t. receive time?Gmail API - 有没有办法按 w.r.t 的排序顺序列出邮件?收到时间?
【发布时间】:2017-03-28 14:18:04
【问题描述】:

有没有办法使用 Google Mail API 按接收时间排序列出 Gmail 邮件?

虽然 API 按时间倒序返回邮件。对于像 10 封邮件这样非常小的数据集,我发现存在差异。

邮件列表API的响应:

 {
 "messages": [
  {
   "id": "15afb61b8d220a19",
   "threadId": "15afb61b8d220a19"
  },
  {
   "id": "15afb618f941d73f",
   "threadId": "15afb618f941d73f"
  },
  {
   "id": "15afb6174711c7af",
   "threadId": "15afb6174711c7af"
  },

  ...
  ...
  {
   "id": "159bc283f63d5eb7",
   "threadId": "159bc283f63d5eb7"
  },
  {
   "id": "159bc283dc97cd87",
   "threadId": "159bc283dc97cd87"
  },
  {
   "id": "159bc283d95f097c",
   "threadId": "159bc283d95f097c"
  }
 ],
 "resultSizeEstimate": 9
}

回复的第一条消息(最近的):

Id: 15afb61b8d220a19
Date: "Thu, 23 Mar 2017 19:06:08 +0530" 
internalDate: "1490276168000"

列表中的第二封邮件:

Id: 15afb618f941d73f
Date: "Thu, 23 Mar 2017 19:05:58 +0530"
internalDate: "1490276158000"

... ... 列表中倒数第二个邮件:

Id: 159bc283dc97cd87 
Date: "Fri, 20 Jan 2017 05:54:30 -0800" 
internalDate: "1484920470000"

列表中的最后一封邮件(应该是最旧的,但不是):

Id: 159bc283d95f097c
Date: "Fri, 20 Jan 2017 05:54:31 -0800"
internalDate: "1484920471000"

【问题讨论】:

    标签: google-api gmail gmail-api


    【解决方案1】:

    我猜这是基于 thread 的预期行为。

    ...messages.list 不返回日期 ASC 或日期 DESC。据我所知,它们是随机返回的。

    您还可以使用search syntax 按电子邮件日期进行过滤。

    【讨论】:

    • 如果我使用搜索语法过滤两个日期之间的消息,那么返回的消息是否排序?
    • 我尝试使用更新的,后等搜索语法为同一组。但结果的顺序相同,即未完全排序。
    【解决方案2】:

    似乎是一个应该提供但显然没有的参数。不过,您自己对结果进行排序还是很容易的。

    如果你使用 php,你可以使用usort()。首先从您的列表中构建一个消息数组,使用internalDate 作为键(看起来您已经这样做了)。如果您希望它们按降序排列:

    usort($messages, function($a, $b) {
        return $b['internalDate'] <=> $a['internalDate'];
    });
    

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 1970-01-01
      • 2011-07-22
      • 2020-02-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      相关资源
      最近更新 更多