获取有关 Google Chat 聊天室活动的详细信息

本指南介绍了如何使用 Google Chat API 的 SpaceEvent 资源中的 get() 方法来获取 Google Chat 聊天室中某个事件的详细信息。

SpaceEvent 资源表示聊天室或其子资源(例如消息、表情回应和成员资格)发生了更改。如需了解支持的事件类型,请参阅 SpaceEvent 资源eventType 字段参考文档。

您可以请求最多 28 天前的活动。该事件包含已更改资源的最新版本。例如,如果您请求有关新消息的事件,但该消息后来更新了,服务器会在事件载荷中返回更新后的 Message 资源。

如需调用此方法,您必须使用用户身份验证。如需获取活动,经过身份验证的用户必须是相应活动发生所在空间的成员。

前提条件

Node.js

Python

获取有关空间事件(用户身份验证)的详细信息

如需获取 Google Chat 中聊天室活动的详细信息,请在请求中传递以下内容:

  • 指定支持请求中事件类型的授权范围。 最佳实践是选择最严格的范围,同时确保应用能够正常运行。
  • 调用 GetSpaceEvent() 方法,并传递要获取的会议室事件的 name

以下示例获取了空间事件:

Node.js

此 Node.js 代码示例使用 Chat RPC API

chat/client-libraries/cloud/get-space-event-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

// Replace SCOPE_NAME here with an authorization scope based on the event type
const USER_AUTH_OAUTH_SCOPES = ['SCOPE_NAME'];

// This sample shows how to get space event with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and SPACE_EVENT_NAME here
    name: 'spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
  };

  // Make the request
  const response = await chatClient.getSpaceEvent(request);

  // Handle the response
  console.log(response);
}

await main();

如需运行此示例,请替换以下内容:

  • SCOPE_NAME:基于事件类型的授权范围。例如,如果您要获取有关新会员资格的聊天室事件,请使用 chat.memberships.readonly 范围,格式为 https://www.googleapis.com/auth/chat.memberships.readonly。您可以通过 ListSpaceEvents() 方法获取事件类型。如需了解如何使用此方法,请参阅列出会议室中的活动
  • SPACE_NAME:来自空间 name 的 ID。您可以通过调用 ListSpaces() 方法或从空间的网址中获取 ID。
  • SPACE_EVENT_NAME:空间事件的 name 中的 ID。您可以通过 ListSpaceEvents() 方法获取 ID。如需了解如何使用此方法,请参阅列出会议室中的活动

Chat API 会返回一个 SpaceEvent 实例,其中包含有关活动的详细信息。

获取有关聊天室活动的详细信息(Chat 应用身份验证)

应用身份验证需要管理员一次性批准

如需使用 Chat REST API 从具有应用身份验证的聊天室获取聊天室事件的详细信息,请在请求中传递以下内容:

  • 指定一个或多个授权范围,以支持请求中的每种事件类型。最佳实践是,选择最严格的范围,同时仍允许您的应用正常运行。如需详细了解如何选择范围,请参阅身份验证和授权概览
    • https://www.googleapis.com/auth/chat.app.memberships
    • https://www.googleapis.com/auth/chat.app.messages.readonly
    • https://www.googleapis.com/auth/chat.app.spaces
  • spaceEvents 资源调用 get 方法
  • 传递空间的 name 以获取活动详情。

创建 API 密钥

如需调用开发者预览版 API 方法,您必须使用非公开的开发者预览版 API 发现文档。如需对请求进行身份验证,您必须传递 API 密钥。

如需创建 API 密钥,请打开应用的 Google Cloud 项目并执行以下操作:

  1. 在 Google Cloud 控制台中,依次前往菜单 > API 和服务 > 凭据

    进入“凭据”页面

  2. 依次点击创建凭据 > API 密钥
  3. 系统会显示您的新 API 密钥。
    • 点击“复制”图标 即可复制 API 密钥,以便在应用的代码中使用。您还可以在项目的凭据的“API 密钥”部分中找到 API 密钥。
    • 为防止未经授权的使用,我们建议您限制 API 密钥可用于哪些位置和 API。如需了解详情,请参阅添加 API 限制

编写调用 Chat API 的脚本

以下是使用应用身份验证Chat REST API 获取聊天室事件详细信息的方法:

Python

此 Python 代码示例使用 Chat REST API

  1. 在工作目录中,创建一个名为 chat_spaceevents_get_app.py 的文件。
  2. chat_spaceevents_get_app.py 中添加以下代码:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Define your app's authorization scopes.
    # Set authorization scopes based on the
    # event type. For example, if you are getting a space event
    # about a new membership, use the `chat.app.memberships.readonly` scope.
    #
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.app.memberships",
              "https://www.googleapis.com/auth/chat.app.messages.readonly",
              "https://www.googleapis.com/auth/chat.app.spaces"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then lists space events from a specified space.
        '''
    
        # Specify service account details.
        creds = (
            service_account.Credentials.from_service_account_file('credentials.json')
            .with_scopes(SCOPES)
        )
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().spaceEvents().get(
    
            # The space to get event details from.
            #
            # Replace SPACE_NAME with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            name='spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,替换以下内容:

    • API_KEY:您创建的用于为 Chat API 构建服务端点的 API 密钥。
    • SPACE_NAME:聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或从聊天室的网址中获取。
    • SPACE_EVENT_NAME:空间事件的 name 中的 ID。您可以通过 ListSpaceEvents() 方法获取 ID。如需了解如何使用此方法,请参阅列出会议室中的活动
  4. 在工作目录中,构建并运行示例:

    python3 chat_spaceevents_get_app.py

Chat API 会返回有关新会员和消息的分页聊天室事件列表