從聊天室中移除成員

本指南說明如何使用 Google Chat API 的 Membership 資源中的 delete() 方法,從即時通訊室移除成員 (也稱為刪除成員資格)。如果聊天室中只有一位管理員,就無法移除該管理員。請先指派其他使用者擔任聊天室管理員,再移除這些成員。

如果您是 Google Workspace 管理員,可以從 Google Workspace 機構的任何聊天室中移除使用者、Google 群組或 Chat 應用程式。

Membership 資源代表使用者或 Google Chat 應用程式是否受邀加入、參與或缺席聊天室。

必要條件

Node.js

  • Google Chat 聊天室。如要使用 Google Chat API 建立聊天室,請參閱「建立聊天室」。如要在 Chat 中建立群組,請參閱說明中心文件

以使用者身分從聊天室移除成員

如要從具有使用者驗證的聊天室中移除使用者、Google 群組或 Chat 應用程式,請在要求中傳遞下列項目:

  • 指定 chat.memberships 授權範圍。 授權使用者必須具備從聊天室移除使用者或 Google 群組的權限。如要移除 Chat 應用程式,請指定chat.memberships.app授權範圍 (應用程式只能刪除自己的成員資格,無法刪除其他應用程式的成員資格)。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。
  • 呼叫 DeleteMembership() 方法。
  • 傳遞要刪除的會員資格 name。如果成員是聊天室中唯一的管理員,請先指派其他使用者擔任管理員,再刪除該成員。

以下說明如何透過 使用者驗證刪除會員方案:

Node.js

chat/client-libraries/cloud/delete-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.delete'];

// This sample shows how to delete a space with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    name: 'spaces/SPACE_NAME'
  };

  // Make the request
  const response = await chatClient.deleteSpace(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

如要執行這個範例,請替換下列項目:

  • SPACE_NAME:來自聊天室的 ID name。 您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:成員的 ID,來自 name。 您可以呼叫 ListMemberships() 方法取得 ID。

如果成功,回應主體會傳回含有 'state': 'NOT_A_MEMBER' 的成員資格,表示該成員已離開空間。

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

以 Chat 應用程式身分從聊天室中移除成員

應用程式驗證需要管理員核准一次。

如要從應用程式驗證的即時通訊空間中移除使用者、Google 群組或 Chat 應用程式,請在要求中傳遞下列項目:

  • 指定 chat.app.memberships 授權範圍。只有在 Chat 應用程式建立的聊天室中,才能刪除聊天室管理員的成員資格。
  • 呼叫 membership 資源的 delete 方法
  • 傳遞要刪除的會員資格 name。如果成員是聊天室中唯一的管理員,請先指派其他使用者擔任管理員,再刪除該成員。

建立 API 金鑰

如要呼叫開發人員預先發布版 API 方法,您必須使用非公開的開發人員預先發布版 API 探索文件。如要驗證要求,您必須傳遞 API 金鑰。

如要建立 API 金鑰,請開啟應用程式的 Google Cloud 專案,然後執行下列操作:

  1. 在 Google Cloud 控制台中,依序前往「選單」圖示 >「API 和服務」 >「憑證」

    前往「憑證」

  2. 依序按一下「建立憑證」>「API 金鑰」
  3. 系統會顯示新的 API 金鑰。
    • 按一下「複製」圖示 ,即可複製 API 金鑰,在應用程式的程式碼中使用。您也可以在專案憑證的「API 金鑰」部分找到 API 金鑰。
    • 按一下「限制金鑰」,即可更新進階設定並限制 API 金鑰的使用方式。詳情請參閱「套用 API 金鑰限制」一文。

編寫呼叫 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, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # 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. 在程式碼中,請替換下列項目:

    • API_KEY:您建立的 API 金鑰,用於建構 Chat API 的服務端點。

    • SPACE:聊天室名稱,可透過 Chat API 中的 spaces.list 方法或聊天室網址取得。

    • MEMBER:成員名稱,可透過 Chat API 中的 spaces.members.list 方法取得。如要刪除應用程式的成員資格,請將 MEMBER 替換為 app

  4. 在工作目錄中,建構並執行範例:

    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 管理員身分呼叫這個方法,請按照下列步驟操作:

  • 使用使用者驗證呼叫方法,並指定支援使用管理員權限呼叫方法的授權範圍
  • 在要求中,將查詢參數 useAdminAccess 指定為 true

如需更多資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。

限制和注意事項

  • 透過應用程式驗證,Chat 應用程式可以移除使用者,但無法移除 Google 群組或 Chat 應用程式。