有没有一种方法可以使用 GMAIL API 从 Gmail 的收件箱中选择多封电子邮件,并在一个请求中删除它们。例如:我选择了 3 封邮件来删除,并且仅在一个请求中删除它们。
在 GMAIL API 中我发现了这个
service.users().threads().delete(userId, threadId).execute();
其中我必须一一传递邮件的message id 才能删除,这将消耗大量时间。预先感谢
Gmail 提供 API 可以一次删除多封电子邮件,您可以在请求正文中传递多个 id。这是 API 参考。
https://developers.google.com/gmail/api/v1/reference/users/messages/batchDelete
编辑:添加示例 Ruby 代码
我使用 google-api-client v0.8.2 gem 进行 API 调用。这是示例代码。
require 'google/api_client'
@client = Google::APIClient.new({application_name: 'MyApp', application_version: 'MyAppVersion'})
@client.authorization.access_token = MY_ACCESS_TOKEN
@service = @client.discovered_api('gmail')
payload = {ids: ['id_1', 'id_2']} # You can add multiple ids here
result = @client.execute(
:api_method => @service.users.messages.batch_delete,
:parameters => {userId: 'me'},
:headers => {'Content-Type' => 'application/json'},
:body => payload.to_json
)
puts result.success? # true/false
注意: 示例代码中使用的 API 不会将您的消息移至垃圾箱,而是会永久删除您的消息。
我喜欢@Sambit的答案,但对于那些虔诚地使用python的人来说,它的service.users().message().batch Delete(userId=userid).body(jsonIds i.e. {'ids':'id', 'id', ' id'})花了我一段时间才弄清楚,我希望我能节省别人的时间