更新 Google Chat 聊天室中的使用者和成員身分

本指南說明如何使用 Google Chat API 的 Membership 資源中的 update() 方法,變更成員資格的屬性,例如將聊天室成員變更為聊天室管理員,或將聊天室管理員變更為聊天室成員。

如果您是 Google Workspace 管理員,可以呼叫 update() 方法,更新 Google Workspace 機構中任何空間的成員資格。

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

必要條件

Node.js

更新會員方案

如要更新空間成員資格,請在要求中傳遞下列項目:

  • 指定授權範圍:
  • 呼叫 UpdateMembership() 方法。
  • membership 做為 Membership 的執行個體傳遞,並使用下列項目:
    • name」欄位設為要更新的成員資格,包括聊天室 ID 和成員 ID。
    • 要更新的成員資格欄位設為新值。
  • 傳遞 updateMask 以指定要更新的會員方案項目,包括:
    • role:使用者在 Chat 聊天室中的角色,決定了他們在該聊天室中可執行的動作。可能的值包括:
      • ROLE_MEMBER:聊天室成員。使用者具備基本權限,例如傳送訊息至即時通訊空間。在 1:1 和未命名的群組對話中,所有人都具備這個角色。
      • ROLE_MANAGER:聊天室管理員。使用者擁有所有基本權限,以及可管理空間的管理權限,例如新增或移除成員。僅支援 spaceTypeSPACE 的聊天室 (已命名的聊天室)。

以使用者身分將一般聊天室成員設為聊天室管理員

下列範例會使用使用者驗證呼叫 Chat API,並指定 role 做為 ROLE_MANAGER,將一般聊天室成員設為聊天室管理員:

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    membership: {
      // Replace SPACE_NAME and MEMBER_NAME here
      name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
      // Replace ROLE_NAME here with ROLE_MEMBER or ROLE_MANAGER
      role: 'ROLE_NAME'
    },
    updateMask: {
      // The field paths to update.
      paths: ['role']
    }
  };

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

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

main().catch(console.error);

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

  • SPACE_NAME:來自聊天室的 ID。name。您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:會員方案的 ID,來自會員方案的 name。 您可以呼叫 ListMemberships() 方法取得 ID,也可以從使用 Chat API 建立成員資格後非同步傳回的回應本文中取得 ID。
  • ROLE_NAME:更新後的角色,ROLE_MANAGER

Google Chat API 會將指定成員資格更新為聊天室管理員,並傳回 Membership 的執行個體。

以使用者身分將聊天室管理員設為一般成員

下列範例會使用使用者驗證呼叫 Chat API,並指定 role 做為 ROLE_MEMBER,將聊天室管理員設為一般聊天室成員:

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    membership: {
      // Replace SPACE_NAME and MEMBER_NAME here
      name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
      // Replace ROLE_NAME here with ROLE_MEMBER or ROLE_MANAGER
      role: 'ROLE_NAME'
    },
    updateMask: {
      // The field paths to update.
      paths: ['role']
    }
  };

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

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

main().catch(console.error);

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

  • SPACE_NAME:來自聊天室的 ID。name。您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。
  • MEMBER_NAME:會員方案的 ID,來自會員方案的 name。 您可以呼叫 ListMemberships() 方法取得 ID,也可以從使用 Chat API 建立成員資格後非同步傳回的回應本文中取得 ID。
  • ROLE_NAME:更新後的角色,ROLE_MEMBER

Google Chat API 會將指定成員資格更新為聊天室管理員,並傳回 Membership 的執行個體。

以 Chat 應用程式身分將一般聊天室成員設為聊天室管理員

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

建立 API 金鑰

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

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

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

    前往「憑證」

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

編寫呼叫 Chat API 的指令碼

以下範例使用應用程式驗證呼叫 Chat API,方法是在 body 中指定 role 做為 ROLE_MANAGER,藉此將一般聊天室成員設為聊天室管理員。body 會指定更新後的成員屬性:

Python

  1. 在工作目錄中,建立名為 chat_membership_update_to_manager_app.py 的檔案。
  2. chat_membership_update_to_manager_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 updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MANAGER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在程式碼中,請替換下列項目:

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

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

    • MEMBERSHIP:成員名稱,可透過 Chat API 中的 spaces.members.list 方法取得。

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

    python3 chat_membership_update_to_manager_app.py

以 Chat 應用程式身分將聊天室管理員設為一般成員

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

建立 API 金鑰

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

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

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

    前往「憑證」

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

編寫呼叫 Chat API 的指令碼

下列範例使用應用程式驗證呼叫 Chat API,在 body 中指定更新後的成員屬性,並將 role 設為 ROLE_MEMBER,將聊天室管理員設為一般成員:

Python

  1. 在工作目錄中,建立名為 chat_membership_update_to_member_app.py 的檔案。
  2. chat_membership_update_to_member_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 via user credentials,
        then updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MEMBER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在程式碼中,請替換下列項目:

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

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

    • MEMBERSHIP:成員名稱,可透過 Chat API 中的 spaces.members.list 方法取得。

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

    python3 chat_membership_update_to_member_app.py

以 Google Workspace 管理員身分更新成員資格

如果您是 Google Workspace 管理員,可以呼叫 update() 方法,更新 Google Workspace 機構中任何空間的成員資格。

如要以 Google Workspace 管理員身分呼叫這個方法,請按照下列步驟操作:

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

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