如何自动化开启Google Workspace部分组织单元的外部聊天设置?
解决方案
方法一:使用GAM(修正命令)
你之前可能没找对GAM的对应命令,GAM支持批量更新组织单元的外部Chat设置,具体操作如下:
- 单个OU更新:
gam update orgunit "/部门/子部门" chat external on - 批量更新(通过OU列表文件):
- 准备文本文件
ou_list.txt,每行写入目标OU的完整路径(例如/市场部、/技术部/研发组) - 执行批量命令:
gam csv ou_list.txt gam update orgunit ~orgunit_path chat external on - 准备文本文件
方法二:Python脚本调用Admin SDK Directory API
如果需要自定义逻辑,可通过Python结合Google API Client Library实现批量更新:
安装依赖包:
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib编写执行脚本(需提前创建并下载Workspace管理员服务账号密钥):
from google.oauth2 import service_account from googleapiclient.discovery import build # 配置参数 SERVICE_ACCOUNT_FILE = 'your-service-account-key.json' ADMIN_EMAIL = 'admin@your-domain.com' OU_PATHS = ["/市场部", "/技术部/研发组", "/运营部"] # 替换为你的目标OU路径 # 初始化API客户端 SCOPES = ['https://www.googleapis.com/auth/admin.directory.orgunit'] credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES, subject=ADMIN_EMAIL) service = build('admin', 'directory_v1', credentials=credentials) # 批量更新OU外部Chat设置 for ou_path in OU_PATHS: try: # 获取目标OU的orgUnitId ou_info = service.orgunits().get(customerId='my_customer', orgUnitPath=ou_path).execute() org_unit_id = ou_info['orgUnitId'] # 提交更新请求 update_body = { 'externalChatEnabled': True } service.orgunits().patch(customerId='my_customer', orgUnitId=org_unit_id, body=update_body).execute() print(f"成功更新OU: {ou_path}") except Exception as e: print(f"更新OU {ou_path}失败: {str(e)}")
注意事项
- 确保使用的管理员账号拥有「组织单元管理」和「Google Chat设置」的权限
- 测试阶段先针对单个OU验证效果,再执行批量操作
- 使用服务账号时,需在Workspace Admin控制台为其启用Domain-Wide Delegation,并授权对应的API范围
内容的提问来源于stack exchange,提问作者Hoàng Đỗ Phương
相关产品推荐
相关产品推荐

