列出聊天室中的成员

本指南介绍了如何对 membership 资源使用 list 方法 使用 Google Chat API,将聊天室中的成员作为可过滤的分页列表列出 一些会员功能列出成员身份 应用身份验证 列出 Chat 应用拥有的聊天室中的成员资格 对 Chat 扩展应用(但不包括在内)的访问权限,包括 自己的数据。列出成员身份 用户身份验证 列出经过身份验证的用户有权访问的聊天室中的成员资格。

通过 Membership 资源 表示是否会邀请真人用户或 Google Chat 应用; 聊天室中的部分内容,或聊天室中缺失的内容。

前提条件

Python

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

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

列出聊天室中的用户、Google 群组和 Chat 应用 请在请求中传递以下内容:

以下示例列出了公开范围: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 替换为聊天室名称, 您可以从 spaces.list 方法 或通过聊天室网址发送。

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

    python3 chat_member_list_user.py
    

Google Chat API 会从 指定空格。

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

列出聊天室中的用户和 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 替换为聊天室名称, 您可以从 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 方法