获取有关聊天室的详细信息

本指南介绍了如何对getSpace Google Chat API,用于查看聊天室的详细信息,例如其显示名称、说明 和准则。

通过 Space 资源 代表用户和 Chat 扩展应用能够发送消息的位置, 共享文件和协作聊天室分为以下几种类型:

  • 私信 (DM) 是指两位用户或一位用户与 Chat 应用。
  • 群聊是三位或更多用户之间的对话, 聊天应用。
  • 已命名的聊天室是用户永久保留的位置,可供用户发送消息、共享文件 和协作。

身份验证方式 应用身份验证 让 Chat 应用获取 Chat 扩展应用有权在 Google Chat 中访问(例如, (应用所属的聊天室)。身份验证方式 用户身份验证 可让您获取通过身份验证的用户有权访问的聊天室。

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 最新的 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 或更高版本
  • npm 软件包管理工具
  • 最新的 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
    npm install @google-cloud/local-auth @googleapis/chat
    

获取聊天室

要在 Google Chat 中获取聊天室,请将以下内容传入您的 请求:

通过用户身份验证获取聊天室详细信息

您可以通过以下方式获取聊天室详细信息: 用户身份验证

Python

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

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets details about a specified space.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().get(
    
              # The space to get.
              #
              # Replace SPACE with a space name.
              # Obtain the space name from the spaces resource of Chat API,
              # or from a space's URL.
              name='spaces/SPACE'
    
          ).execute()
    
        # Prints details about the space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,将 SPACE 替换为聊天室名称, 您可以从 spaces.list 方法 或通过聊天室网址发送。

  4. 在您的工作目录中,构建并运行该示例:

    python3 chat_space_get_user.py
    

Node.js

  1. 在您的工作目录中,创建一个名为 get-space.js 的文件。
  2. get-space.js 中添加以下代码:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Gets details about a Chat space by name.
    * @return {!Object}
    */
    async function getSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.get({name: 'spaces/SPACE'});
    }
    
    getSpace().then(console.log);
    
  3. 在代码中,将 SPACE 替换为聊天室名称, 您可以从 spaces.list 方法 或通过聊天室网址发送。

  4. 在您的工作目录中,运行该示例:

    node get-space.js
    

Chat API 会返回 Space,用于详细说明指定空间。

通过应用身份验证获取聊天室详细信息

您可以通过以下方式获取聊天室详细信息: 应用身份验证

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().get(
    
        # The space to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        name='spaces/SPACE'
    
    ).execute()
    
    print(result)
    
  3. 在代码中,将 SPACE 替换为聊天室名称, 您可以从 spaces.list() 方法(在 Chat API,或通过聊天室网址添加。

  4. 在您的工作目录中,构建并运行该示例:

    python3 chat_space_get_app.py
    

Node.js

  1. 在您的工作目录中,创建一个名为 app-get-space.js 的文件。
  2. app-get-space.js 中添加以下代码:

    const chat = require('@googleapis/chat');
    
    /**
    * Gets details about a Chat space by name.
    * @return {!Promise<!Object>}
    */
    async function getSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.get({name: 'spaces/SPACE'});
    }
    
    getSpace().then(console.log);
    
  3. 在代码中,将 SPACE 替换为聊天室名称, 您可以从 spaces.list 方法 或通过聊天室网址发送。

  4. 在您的工作目录中,运行该示例:

    node app-get-space.js
    

Chat API 会返回 Space 用于详细说明指定空间的方面。