从聊天室中移除成员 (original) (raw)

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

如果您是 Google Workspace 管理员,则可以从 Google Workspace 组织中的任何聊天室移除用户、Google 群组或 Chat 应用。

Membership 资源表示人工用户或 Google Chat 应用是否受邀加入聊天室、是否是聊天室的成员,或者是否不在聊天室中。

前提条件

Node.js

以用户身份从聊天室中移除成员

如需从启用用户身份验证的聊天室中移除用户、Google 群组或聊天应用,请在请求中传递以下内容:

如需删除包含用户身份验证的会员资格,请按以下步骤操作:

Node.js

如需运行此示例,请替换以下内容:

如果成功,响应正文会返回 'state': 'NOT_A_MEMBER' 的会员资格,表明相应会员已不再是相应会议室的成员。

{ "name": "spaces/SPACE_NAME/members/MEMBER_NAME", "state": "NOT_A_MEMBER" }

以 Chat 应用的身份从聊天室中移除成员

应用身份验证需要管理员一次性批准

如需从具有应用身份验证的聊天室中移除用户、Google 群组或 Chat 应用,请在请求中传递以下内容:

编写调用 Chat API 的脚本

如需通过应用身份验证删除会员资格,请按以下步骤操作:

Python

  1. 在工作目录中,创建一个名为 chat_membership_delete_app.py 的文件。
  2. chat_membership_delete_app.py 中添加以下代码:
from google.oauth2 import service_account  
from apiclient.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.app.memberships"]  
def main():  
    '''  
    Authenticates with Chat API using app authentication,  
    then deletes the specified membership.  
    '''  
    # Specify service account details.  
    creds = (  
        service_account.Credentials.from_service_account_file('credentials.json')  
        .with_scopes(SCOPES)  
    )  
    # 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()  
  1. 在代码中,替换以下内容:
    • SPACE:聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或从聊天室的网址中获取该名称。
    • MEMBER:会员名称,您可以通过 Chat API 中的 spaces.members.list 方法获取该名称。如需删除应用的会员资格,请将 MEMBER 替换为 app
  2. 在工作目录中,构建并运行示例:
python3 chat_membership_delete_app.py  

如果成功,响应正文会返回 'state': 'NOT_A_MEMBER' 的成员资格,表明相应成员已不再位于该空间中。

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

以 Google Workspace 管理员身份从聊天室中移除用户或 Google 群组

如果您是 Google Workspace 管理员,可以调用 DeleteMembership() 方法,从 Google Workspace 组织中的任何聊天室移除用户、Google 群组或 Chat 应用。

如需以 Google Workspace 管理员身份调用此方法,请执行以下操作:

如需了解详情和示例,请参阅以 Google Workspace 管理员身份管理 Google Chat 聊天室

限制和注意事项