从聊天室中移除成员

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

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 授权范围(应用只能删除自己的成员资格,而不能删除其他应用的成员资格)。最佳实践是选择限制性最强但仍可应用正常运行的作用域。
  • 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"
}