从聊天室中移除用户或 Google Chat 应用

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

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 授权范围内进行用户身份验证

从聊天室中移除用户或 Chat 应用

如要从聊天室中移除用户或 Chat 应用,请执行以下操作:

  • 如需移除用户,请指定 chat.memberships 授权范围。如需移除 Chat 应用,请指定 chat.memberships.app 授权范围(应用只能删除自己的成员资格,而不能删除其他应用的成员身份)。最佳实践是选择限制性最强的作用域,确保应用仍能正常运行。
  • membership 资源调用 delete 方法
  • 传递要删除的成员资格的 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:聊天室名称,可通过 Chat API 中的 spaces.list 方法或聊天室网址获取。

    • MEMBER:成员资格名称,可通过 Chat API 中的 spaces.members.list 方法获取。如需删除应用的成员资格,请将 MEMBER 替换为 app

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

    python3 chat_membership_delete.py
    

如果成功,响应正文会返回包含 'state': 'NOT_A_MEMBER' 的成员资格,表示该成员已不在聊天室中。

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