能否通过 API 以编程方式从用户的 Gmail 收件箱中删除电子邮件?

Can email be deleted from your users' Gmail inboxes programmatically via API?

Google Workspace 管理员帮助中心的 "Find and delete malicious emails" 页面指出:

Using the investigation tool, you can identify all users in your domain that have received the message (for example, a phishing email). You can then use the investigation tool to delete the email from your users' Gmail inboxes (note that log data might take up to a few minutes before being available in the investigation tool).

据我了解,如果您拥有正确的程序包,则可以使用调查工具从用户的 Gmail 收件箱中删除不需要的电子邮件。

我可以使用 API 通过 Google 应用程序脚本从用户的 Gmail 收件箱中删除电子邮件吗?如果可以,我该怎么做?

目前无法使用调查工具

但是已经存在 feature request for exposing the Google Workspace Investigation Tool programmatically

我建议您给它“加注星标”,以表明 Google 更多人对此功能感兴趣 - 希望这会加速实施。

更新:

在此期间,您可以使用变通方法,例如按照@TheAddonDepot

的建议使用具有域范围委托的服务帐户

可以通过 DwDg(全域委托)实现

您必须创建一个服务帐户并将域范围的权限委托给具有适当范围的该服务帐户。没有针对 Gmail API 的明确说明,但您可以使用管理目录 API 文档中的 this guide

然后您使用 service account credentials with the GAS OAuth2 Library 代表现有用户向 Gmail API 发出呼叫。请注意,您不能使用 Gmail 的内置或高级服务,您必须直接通过 UrlFetchApp.

调用 Gmail REST API