列出聊天室中的成员

本指南介绍了如何针对 Google Chat API 的 membership 资源使用 list 方法,将聊天室中的成员作为可过滤的分页成员列表列出。通过应用身份验证列出成员资格时,也会列出 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 应用配置的授权。商家信息成员资格支持以下两种身份验证方法:

在经过用户身份验证的聊天室中列出成员

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

  • 使用用户身份验证时,请指定 chat.memberships.readonlychat.memberships 授权范围。
  • membership 资源调用 list 方法
  • 如需列出 Google 网上论坛,请将查询参数 showGroups 设置为 true

以下示例列出了对经过身份验证的用户可见的 Google 群组、真人和应用成员。

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 Google Group, human, and app members 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',
    
            # Set this parameter to list Google Groups.
            showGroups = 'true'
    
        ).execute()
    
        # Prints the list of memberships.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,将 SPACE 替换为聊天室名称,您可以从 Chat API 中的 spaces.list 方法或聊天室的网址获取该名称。

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

    python3 chat_member_list_user.py
    

Google Chat API 会返回指定聊天室中 Google 群组成员、真人和应用成员的列表。

在进行应用身份验证的聊天室中列出成员

如需列出经过身份验证的应用有权访问的聊天室中的用户和 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 个空格。最大值为 1000;超过 1000 的值会自动更改为 1000。
  • pageToken:从上一个列表空间调用接收的页面令牌。提供此令牌以检索后续页面。分页时,过滤条件值应与提供页面令牌的调用匹配。传递不同的值可能会导致意外结果。
  • filter:查询过滤条件。需要用户身份验证。如需详细了解受支持的查询,请参阅 spaces.members.list 方法