列出聊天室中的用户和 Google Chat 应用

本指南介绍了如何对 Google Chat API 的 membership 资源使用 list 方法,将聊天室中的用户和 Chat 应用列为聊天室中可过滤的分页成员列表。使用应用身份验证列出成员时,系统会列出 Chat 应用有权访问的聊天室中的成员,但会排除 Chat 应用成员(包括应用自己的成员)。如果使用用户身份验证功能列出成员,系统会列出经过身份验证的用户有权访问的聊天室中的成员。

Membership 资源表示真人用户或 Google Chat 应用是受邀加入聊天室、参与聊天室还是出席聊天室。

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 适用于 Python 的最新 Google 客户端库。如需安装或更新这些插件,请在命令行界面中运行以下命令:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • 一个启用了并配置了 Google Chat API 的 Google Cloud 项目。如需了解相关步骤,请参阅构建 Google Chat 应用
  • 为 Chat 应用配置授权。列表成员资格支持以下两种身份验证方法:

在设置了用户身份验证的聊天室中列出用户和 Chat 应用

如需在经过身份验证的用户有权访问的聊天室中列出用户和 Chat 应用,请在请求中传递以下内容:

以下示例列出了经过身份验证的用户可以看到的真人空间成员(而非聊天室管理员),因为 filter 设置为 ROLE_Member

Python

  1. 在您的工作目录中,创建一个名为 chat_member_list_user.py 的文件。
  2. chat_member_list_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.memberships.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists human space members (but not space managers) in 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().members().list(
    
            # The space for which to list memberships.
            parent = 'spaces/SPACE',
    
            # An optional filter that returns only human space members.
            filter = 'member.type = "HUMAN" AND role = "ROLE_MEMBER"'
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,将 SPACE 替换为聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或聊天室网址获取该名称。

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

    python3 chat_member_list_user.py
    

Google Chat API 会返回指定聊天室中的真人聊天室成员(不包括聊天室管理员)和应用成员的列表。

列出通过应用身份验证的聊天室中的用户和 Chat 应用

如需在经过身份验证的应用有权访问的聊天室中列出用户和 Chat 应用,请在请求中传递以下内容:

以下示例列出了 Chat 应用可见的真人聊天室成员(而非聊天室管理员):

Python

  1. 在您的工作目录中,创建一个名为 chat_member_list_app.py 的文件。
  2. chat_member_list_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().members().list(
    
            # The space for which to list memberships.
            parent = 'spaces/SPACE',
    
            # An optional filter that returns only human space members.
            filter = 'member.type = "HUMAN" AND role = "ROLE_MEMBER"'
    
        ).execute()
    
    print(result)
    
  3. 在代码中,将 SPACE 替换为聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或聊天室网址获取该名称。

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

    python3 chat_member_list_app.py
    

Google Chat API 会返回指定聊天室中的真人聊天室成员(不包括聊天室管理员)列表。

自定义分页或过滤列表

如需列出成员,请传递以下查询参数以自定义对所列成员的分页或过滤:

  • pageSize:要返回的成员资格数量上限。服务返回的值可能会小于此值。如果未指定,则最多返回 100 个空格。最大值为 1,000;大于 1,000 的值将自动更改为 1,000。
  • pageToken:从上一个列表空间调用收到的页面令牌。提供此令牌即可检索后续页面。进行分页时,过滤条件值应与提供页面令牌的调用相匹配。传递其他值可能会导致意外结果。
  • filter:查询过滤条件。需要用户身份验证。如需详细了解受支持的查询,请参阅 spaces.members.list 方法