本指南介绍如何对 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-httplib2 google-auth-oauthlib oauth2client
已发布的 Chat 应用。如需创建和发布 Chat 应用,请参阅构建 Google Chat 应用。
为 Chat 应用配置授权。商家信息成员资格支持以下两种身份验证方法:
通过用户身份验证列出聊天室中的用户和 Chat 应用
如需列出经过身份验证的用户有权访问的聊天室中的用户和 Chat 应用,请在请求中传递以下内容:
- 使用用户身份验证时,指定
chat.memberships.readonly
或chat.memberships
授权范围。 - 对
membership
资源调用list
方法。
以下示例列出了经过身份验证的用户可以看到的人机空间成员(而非聊天室管理员),因为 filter
设置为 ROLE_Member
:
Python
- 在您的工作目录中,创建一个名为
chat_member_list_user.py
的文件。 在
chat_member_list_user.py
中添加以下代码:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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()
在代码中,将
SPACE
替换为聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室网址获取该名称。在您的工作目录中,构建并运行示例:
python3 chat_member_list_user.py
Google Chat API 会返回指定聊天室中的真人聊天室成员(不包括聊天室管理员)和应用成员的列表。
通过应用身份验证列出聊天室中的用户和 Chat 应用
如需在经过身份验证的应用有权访问的聊天室中列出用户和 Chat 应用,请在请求中传递以下内容:
以下示例列出了 Chat 应用可以看到的真人聊天室成员(而非聊天室管理员):
Python
- 在您的工作目录中,创建一个名为
chat_member_list_app.py
的文件。 在
chat_member_list_app.py
中添加以下代码:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # 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)
在代码中,将
SPACE
替换为聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室网址获取该名称。在您的工作目录中,构建并运行示例:
python3 chat_member_list_app.py
Google Chat API 会返回指定聊天室中的真人聊天室成员(不包括聊天室管理员)列表。
自定义分页或过滤列表
如需列出成员,请传递以下查询参数以自定义对所列成员的分页或过滤:
pageSize
:要返回的成员资格数量上限。服务返回的值可能会小于此值。如果未指定,最多返回 100 个空格。最大值为 1,000;大于 1,000 的值会自动更改为 1,000。pageToken
:从上一个列表聊天室调用收到的页面令牌。 提供此令牌以检索后续页面。进行分页时,过滤条件值应与提供页面令牌的调用相匹配。传递其他值可能会导致意外结果。filter
:查询过滤条件。需要用户身份验证。如需详细了解受支持的查询,请参阅spaces.members.list
方法。