Google Chat API 权限问题

问题描述 投票:0回答:1

我创建了一个谷歌聊天机器人,它使用云运行功能连接到人工智能模型。我用它来发送异步消息,因为该模型的工作时间往往超过 30 秒。

我用这段代码做到了:

def send_asynchronous_chat_message(thread_id, body, message_id=None):
    space_id = thread_id.split(",")[1]
    logging.info("space id %s" % space_id)
    space_name = f"spaces/{space_id}"

    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    credentials, project = google.auth.default(scopes=SCOPES)
    chat = build('chat', 'v1', credentials=credentials)

    # update content of an existing message
    if message_id:
        response_obj = chat.spaces().messages().update(
            name=message_id,
            updateMask='text',
            body=body
        ).execute()

    # create a new message
    else:
        response_obj = chat.spaces().messages().create(
            parent=space_name,
            body=body
        ).execute()

    return response_obj.get("name")

当我将它与与 Cloud Run Function 位于同一项目中的聊天 API 一起使用时,它工作正常,但是当我尝试将触发器 URL 添加到另一个组织/项目中的聊天 API 时,我在发送时收到此错误一条消息。

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://chat.googleapis.com/v1/spaces/XXXXXXXXXX/messages?alt=json returned "This Chat app is not a member of this space.". Details: "This Chat app is not a member of this space.">

项目上启用了聊天 API。

这个问题有解决办法吗?无需将我的应用程序投放市场。

python google-cloud-platform google-cloud-functions google-chat
1个回答
0
投票

您需要一个启用了域范围委派的服务帐户。

要为 Google Workspace 中的服务帐号启用全网域委托,您可以: 在 Google Cloud 控制台中创建服务帐号 从 Google Cloud 控制台复制服务帐号的客户端 ID 以超级管理员身份登录 Google 管理控制台 转至菜单 > 安全 > 访问和数据控制 > API 控制 选择管理域范围委派 单击添加新的 输入服务帐户的客户端 ID 在 OAuth 范围字段中输入以逗号分隔的 OAuth 范围列表 点击授权 域范围委派允许服务帐号模拟 Cloud Identity 或 Workspace 帐号中的任何用户。这使得服务帐号可以绕过用户的同意访问用户的 Google Workspace 数据。

© www.soinside.com 2019 - 2024. All rights reserved.