You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API程序化访问及修改Google Workspace Admin Console中Gmail内容合规规则

程序化操作Gmail内容合规规则的方案

可以通过Admin SDK的Gmail Settings API实现对Google Workspace Admin Console中Gmail内容合规规则的全生命周期管理(查询、添加、更新、删除),以下是具体说明:

前置要求

  • 拥有Google Workspace超级管理员权限,或被授予Gmail设置管理权限的管理员账号
  • 在Google Cloud Console中启用Admin SDK API
  • 创建服务账号并完成域范围授权,确保服务账号能代表域内管理员执行API操作

核心操作指南

1. 列出现有内容合规规则

使用users.settings.contentCompliance.list方法,指定目标域名(域级规则用userId="yourdomain.com")即可获取所有规则:

from googleapiclient.discovery import build
from google.oauth2 import service_account

# 配置参数
SERVICE_ACCOUNT_KEY = "path/to/your-service-account-key.json"
ADMIN_EMAIL = "admin@yourdomain.com"
SCOPES = ["https://www.googleapis.com/auth/admin.settings.gmail"]

# 初始化认证
credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_KEY, scopes=SCOPES
)
delegated_creds = credentials.with_subject(ADMIN_EMAIL)

# 构建API服务
gmail_service = build("gmail", "v1", credentials=delegated_creds)

# 调用API获取规则
response = gmail_service.users().settings().contentCompliance().list(
    userId="yourdomain.com"
).execute()

print("现有内容合规规则:", response.get("contentComplianceRules", []))

2. 添加新规则

使用users.settings.contentCompliance.create方法,构造包含触发条件、执行操作的规则体:

# 定义规则内容
new_rule = {
    "name": "内部敏感数据拦截规则",
    "triggers": [
        {
            "type": "KEYWORD",
            "matchType": "CONTAINS",
            "content": ["机密", "内部仅限", "Proprietary"]
        }
    ],
    "actions": [
        {
            "type": "QUARANTINE"  # 隔离邮件,可替换为DELETE/REDIRECT等
        }
    ],
    "scope": {
        "domain": True  # 应用于整个域
    }
}

# 创建规则
create_response = gmail_service.users().settings().contentCompliance().create(
    userId="yourdomain.com", body=new_rule
).execute()

print(f"已创建规则,ID: {create_response['id']}")

3. 更新现有规则

使用users.settings.contentCompliance.update方法,指定规则ID和更新后的规则体:

# 规则ID从列表查询结果中获取
RULE_ID = "your-rule-id"

# 定义更新后的规则内容
updated_rule = {
    "name": "更新后的敏感数据拦截规则",
    "triggers": [
        {
            "type": "KEYWORD",
            "matchType": "CONTAINS",
            "content": ["机密", "内部仅限", "Proprietary", "保密"]
        }
    ],
    "actions": [
        {
            "type": "QUARANTINE"
        }
    ]
}

# 更新规则
update_response = gmail_service.users().settings().contentCompliance().update(
    userId="yourdomain.com", contentComplianceRuleId=RULE_ID, body=updated_rule
).execute()

print("规则已更新:", update_response)

4. 删除规则

使用users.settings.contentCompliance.delete方法,指定规则ID即可删除:

gmail_service.users().settings().contentCompliance().delete(
    userId="yourdomain.com", contentComplianceRuleId=RULE_ID
).execute()

print("规则已删除")

关键注意事项

  • 规则的触发类型支持关键词、正则表达式、敏感数据检测等,操作类型支持隔离、删除、重定向、发送通知等,需与Admin Console中的配置逻辑保持一致
  • 测试阶段建议先在单个用户或测试域验证规则,避免影响域内正常邮件流
  • API调用需严格遵循Google Workspace的API配额限制

内容的提问来源于stack exchange,提问作者shayan amar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 15:22:10