Python调用Google Chat API创建Space时提示应用未找到的问题
解决Google Chat API调用404/403错误:"Google Chat app not found"
问题背景
尝试对接Google Chat API为Odoo实现创建Space、群组、发送消息及添加附件功能,使用Python调用时持续出现403或404错误,错误提示:Google Chat app not found. To create a Chat app, you must turn on the Chat API and configure the app in the Google Cloud console.
已完成以下操作:
- 在GCP中创建项目
- 启用Google Chat API:进入项目>API与服务>已启用的API与服务>Google Chat API>启用
- 进入管理页面>凭据>创建凭据>服务账号
- 填写服务账号名称,授予Owner、Chat Bots Owner角色
- 创建服务账号后,进入密钥>添加新密钥>下载JSON密钥文件并使用
确认API已正确启用且密钥无误,但仍报错,目标是通过服务账号实现组织级集成。
调用代码
from googleapiclient.discovery import build from google.oauth2 import service_account # Replace with your service account key file path SERVICE_ACCOUNT_KEY_FILE = "./fluted-bee-418112-d595ff244760.json" # Scopes for Chat API access SCOPES = ['https://www.googleapis.com/auth/chat.bot', "https://www.googleapis.com/auth/chat.spaces.create", "https://www.googleapis.com/auth/chat.messages"] def create_space(credentials, name): service = build("chat", "v1", credentials=credentials) print(service) body = { "displayName": name, "spaceType": "SPACE", } try: response = service.spaces().create(body=body).execute() print(f"Space created: {response['name']}") # return response["name"] except Exception as e: print(f"Error creating space: {e}") return None def get_service_account_credentials(): credentials = service_account.Credentials.from_service_account_file( filename=SERVICE_ACCOUNT_KEY_FILE, scopes=SCOPES) return credentials if __name__ == "__main__": # Get credentials using service account credentials = get_service_account_credentials() # Create space using authorized credentials space_name = create_space(credentials, "My Space") if not space_name: exit()
完整错误信息
<googleapiclient.discovery.Resource object at 0x00000151E77DD5B0> Error creating space: <HttpError 404 when requesting https://chat.googleapis.com/v1/spaces?alt=json returned "Google Chat app not found. To create a Chat app, you must turn on the Chat API and configure the app in the Google Cloud console.". Details: "[{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'To turn on the Google Chat API, go to the Google Chat API page in the Google Cloud console and click Enable.', 'url': 'https://console.cloud.google.com/marketplace/product/google/chat.googleapis.com'}]}, {'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'To configure your Chat app, go to the Google Chat API Configuration tab in the Google Cloud console.', 'url': 'https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat'}]}, {'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'To learn about creating Google Chat apps, visit the documentation.', 'url': 'https://developers.google.com/chat'}]}]">
解决方案
核心问题
虽然启用了Google Chat API,但未在GCP中完成Chat应用的配置,导致服务账号无法关联到有效的Chat应用实例,触发"app not found"错误。
修复步骤
完成Chat应用配置
进入GCP控制台对应项目的Google Chat API页面:项目>API与服务>已启用的API与服务>找到Google Chat API>进入后切换到「配置」标签页- 填写应用基本信息(名称、头像、描述)
- 设置应用类型:根据组织级需求选择「工作区集成」
- 勾选所需权限(如创建空间、发送消息)
- 保存配置,等待5-10分钟生效
调整权限范围与服务账号授权
- 若需组织级操作,代码中添加管理员级权限,例如
https://www.googleapis.com/auth/chat.admin.spaces.create - 在Google Workspace管理员后台,为该服务账号配置域范围委派,授予对应的Chat API权限范围
- 确保服务账号在Google Workspace中被授予Chat空间管理员等相关角色
- 若需组织级操作,代码中添加管理员级权限,例如
重新测试代码
配置完成后重新运行代码,验证是否能正常创建Space。
内容的提问来源于stack exchange,提问作者Mubashir Rehman
相关产品推荐
相关产品推荐

