从聊天室中移除成员

本指南介绍了如何对 membership 资源使用 delete 方法 使用 Google Chat API 将成员从聊天室中移除,这也称为删除 会员资格。如果聊天室管理员是唯一的聊天室管理员,则无法移除 请先将其他用户分配为聊天室管理员,然后再移除这些用户 会员。

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

前提条件

Python

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

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • 一个已启用并配置了 Google Chat API 的 Google Cloud 项目。有关具体步骤,请参阅 构建 Google Chat 应用
  • 为 Chat 应用配置的授权。正在删除 成为会员 用户身份验证 替换为 用户的 chat.membershipschat.memberships.app 授权范围 其有权删除指定成员资格的用户。

从聊天室中移除成员

如需从群组中移除用户、Google 群组或 Chat 应用,请执行以下操作: 空格:

  • 如需移除用户或 Google 群组,请指定 chat.memberships 授权 范围。如要移除 Chat 扩展应用,请指定 chat.memberships.app 授权范围(应用只能删除自己的授权范围) 会员;而不是其他应用的情况)。最佳做法是选择 限制作用域,使应用仍能正常运行。
  • 调用 delete 方法membership 资源
  • 传递要删除的成员资格的 name。如果成员资格属于 仅聊天室管理员,请先将其他用户分配为聊天室管理员 删除此成员资格。

要删除会员资格,请按以下步骤操作:

Python

  1. 在您的工作目录中,创建一个名为 chat_membership_delete.py 的文件。
  2. chat_membership_delete.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.app"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes the specified membership.
        '''
    
        # 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().delete(
    
            # The membership to delete.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBER with a membership name.
            # Obtain the membership name from the memberships resource of
            # Chat API. To delete a Chat app's membership, replace MEMBER
            # with app; an alias for the app calling the API.
            name='spaces/SPACE/members/MEMBER'
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        # When deleting a membership, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中进行以下替换:

    • SPACE:聊天室名称,您可以从中获取 spaces.list 方法 或通过聊天室网址发送。

    • MEMBER:您可以获取的会员名称 (通过 spaces.members.list 方法) 。要删除应用的成员资格,请将 app会员价为 MEMBER

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

    python3 chat_membership_delete.py
    

如果成功,响应正文将返回具有 'state': 'NOT_A_MEMBER':表示该成员已不在聊天室中。

{
    "name": "spaces/SPACE/members/MEMBER",
    "state": "NOT_A_MEMBER"
}