我正在关注 this article通过服务帐户在 Google Chat 中创建消息。但是,当我编译代码时,收到以下消息:
Traceback (most recent call last): File "googlechat-api.py", line 27, in resp = chat.spaces().messages().create( File "C:\Users\tdi5539\Anaconda3\lib\site-packages\googleapiclient_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "C:\Users\tdi5539\Anaconda3\lib\site-packages\googleapiclient\http.py", line 935, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 404 when requesting https://chat.googleapis.com/v1/spaces/AAAAcE3QeHo/messages?alt=json returned "Invalid project number.". Details: "Invalid project number.">
谁能为我解释一下是什么问题?
顺便说一下,这是我的代码:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
scopes = 'https://www.googleapis.com/auth/chat.bot'
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'credentials.json', scopes)
chat = build('chat', 'v1', http=credentials.authorize(Http()))
resp = chat.spaces().messages().create(
parent='spaces/AAAAcE3QeHo', # use your space here
body={'text': 'Test message'}).execute()
print(resp)
最佳答案
您需要设置您的机器人,并且您只能使用 Google Workspace aka GSuite 帐户来设置机器人
就我而言,我使用 @gmail.com 帐户启动了该项目,并且必须将来自 Google Workspace @mycompany.com 的用户添加到该项目并从该帐户设置 Bot。
设置 Google Chat Bots 的链接应该是这样的:
https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat?authuser=1&project=[your-project]&supportedpurview=project
关于python - 使用 google chat api 时项目编号无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68313592/