使用gmail API移动所有早于X的线程 [英] Move all threads older than X with gmail api

查看:58
本文介绍了使用gmail API移动所有早于X的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹Archive,我想在其中存储给定日期之前的所有电子邮件.

I have a folder, Archive where I'd like to store all of my emails from before a given date.

在Gmail网络用户界面中,您可以执行以下搜索来完成此操作:在2012年1月1日之前在收件箱中",选择全部,然后从下拉列表中移动".

In the Gmail web UI, you can accomplish this by doing a search like "in:inbox before:2012/01/01", select all, then "move" from the dropdown.

我想用gmail api做到这一点.我该怎么做?我是否需要进行查询,然后遍历每个线程来移动它?还是有更好的方法?

I'd like to do this with the gmail api. How can I accomplish this? Will I need to do the query then iterate through each thread to move it? Or is there a better way?

代码段/示例的奖励点,或链接到api文档的相关部分.

Bonus points for a code snippet / example or link to the relevant section of api docs.

推荐答案

您是对的.您需要获取每个threadId并一次将其移动一个(可以通过批处理请求来实现,但是无论如何它们都将作为单个请求计入您的配额).可能是另一个我不知道的API的解决方案.

You are right on the money. You need to get every threadId and move them one at a time (can be achieved by a batch request, but they will count as individual requests to your quota anyway). Might be another solution with another API I don't know of.

但是使用Gmail API,您可以执行以下操作.

But with the Gmail API you would do the following.

从收件箱中获取在给定日期之前收到的所有threadId.

GET https://www.googleapis.com/gmail/v1/users/me/threads?fields=nextPageToken%2Cthreads%2Fid&q=n%3Ainbox+before%3A2012%2F01%2F01&key={YOUR_API_KEY}

哪个会给您以下数据:

{
 "threads": [
  {
   "id": "12345"
  },
  {
   "id": "123456"
  },
  {
    .
    .
    .
  }
 ],
 "nextPageToken": "112233"
}

在下一个请求中使用nextPageToken以获取更多threadId.

GET https://www.googleapis.com/gmail/v1/users/me/threads?pageToken=112233&fields=nextPageToken%2Cthreads%2Fid&q=n%3Ainbox+before%3A2012%2F01%2F01&key={YOUR_API_KEY}

重复上一步,直到响应中没有nextPageToken.

您现在拥有在给定日期(2012/01/01)之前处于活动状态的所有线程.

You now have all the threads that were active before your given date (2012/01/01).

一个一个地修改它们.删除INBOX标签并添加存档标签

POST https://www.googleapis.com/gmail/v1/users/me/threads/12345/modify?key={YOUR_API_KEY}

{
 "addLabelIds": [
  "Archive"
 ],
 "removeLabelIds": [
  "INBOX"
 ]
}

同样,这可以在批量请求中完成,但是仍然可以数不尽相同.

Again, this can be done in a batch request, but will still count as many separate ones.

取决于您拥有的线程数,您每秒可能最终完成比Google允许的请求更多的请求.如果遇到错误,请稍等片刻,然后继续.

Depending on how many threads you have, you might end up doing more requests per second than Google allows. If you get an error, wait for a moment in your code, and continue.

这篇关于使用gmail API移动所有早于X的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆