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 スペース内のユーザーのロール。スペースで許可されるアクションを決定します。権限の詳細については、Chat API リファレンス ドキュメントの MembershipRole をご覧ください。表示される値は次のとおりです。
      • ROLE_MEMBER: スペースのメンバー。Chat UI では、このロールは [メンバー] と呼ばれます。
      • ROLE_ASSISTANT_MANAGER: スペースの管理者。Chat UI では、このロールは [管理者] と呼ばれます。
      • ROLE_MANAGER: スペースのオーナー。Chat UI では、このロールは [オーナー] と呼ばれます。

メンバーをオーナーに変更する(ユーザー認証)

次の例では、 ユーザー認証 を使用して Chat API を呼び出し、roleROLE_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);
}

await main();

サンプルを実行するには、次のように置き換えます。

  • SPACE_NAME: スペースの nameからの ID。 ID は、 ListSpaces() メソッドを呼び出すか、スペースの URL から取得できます。
  • MEMBER_NAME: メンバーシップの nameからの ID。 ID は、 ListMemberships() メソッドを呼び出すか、Chat API でメンバーシップを 非同期で作成した後に返されるレスポンス本文から取得できます。
  • ROLE_NAME: 更新されたロール ROLE_MANAGER。 この値は、 MembershipRole の任意の値に設定できます。 たとえば、通常のメンバーをスペースの管理者に変更するには、ROLE_NAMEROLE_ASSISTANT_MANAGER に変更します。

Google Chat API は、指定されたメンバーシップをスペースのオーナーに更新し、 an instance of Membership を返します。

オーナーをメンバーに変更する(ユーザー認証)

次の例では、 ユーザー認証 を使用して Chat API を呼び出し、roleROLE_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);
}

await main();

サンプルを実行するには、次のように置き換えます。

  • SPACE_NAME: スペースの nameからの ID。 ID は、 ListSpaces() メソッドを呼び出すか、スペースの URL から取得できます。
  • MEMBER_NAME: メンバーシップの nameからの ID。 ID は、 ListMemberships() メソッドを呼び出すか、Chat API でメンバーシップを 非同期で作成した後に返されるレスポンス本文から取得できます。
  • ROLE_NAME: 更新されたロール ROLE_MEMBER

Google Chat API は、指定されたメンバーシップをスペースのオーナーに更新し、 an instance of Membership を返します。

メンバーをオーナーに変更する(Chat アプリ認証)

アプリ認証には、管理者の 1 回限りの 承認が必要です

Chat API を呼び出すスクリプトを作成する

次の例では、 アプリ認証 を使用して Chat API を呼び出し、更新されたメンバーシップ属性を指定する bodyroleROLE_MANAGER に指定して、通常のスペースのメンバーをスペースのオーナーにします。

Python

  1. 作業ディレクトリに chat_membership_update_to_owner_app.py という名前のファイルを作成します。
  2. chat_membership_update_to_owner_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 owner.
        '''
    
        # 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().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',
    
            # Replace ROLE with a MembershipRole value.
            # Obtain the MembershipRole values from the membership of Chat API.
            body={'role': 'ROLE'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで、次のように置き換えます。

    • SPACE: スペース名。Chat API の spaces.list メソッド またはスペースの URL から取得できます。

    • MEMBERSHIP: メンバーシップ名。Chat API の spaces.members.list メソッド から取得できます。

    • ROLE: 更新されたロール ROLE_MANAGER。 この値は、 MembershipRole の任意の値に設定できます。 たとえば、通常のメンバーをスペースの管理者に変更するには、 ROLEROLE_ASSISTANT_MANAGER に変更します。

  4. 作業ディレクトリで、サンプルをビルドして実行します。

    python3 chat_membership_update_to_owner_app.py

オーナーをメンバーに変更する(Chat アプリ認証)

アプリ認証には、管理者の 1 回限りの 承認が必要です

Chat API を呼び出すスクリプトを作成する

次の例では、 アプリ認証 を使用して Chat API を呼び出し、更新されたメンバーシップ属性を指定する bodyroleROLE_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 owner to change
        it to a regular member.
        '''
    
        # 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().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. コードで、次のように置き換えます。

  4. 作業ディレクトリで、サンプルをビルドして実行します。

    python3 chat_membership_update_to_member_app.py

Google Workspace 管理者としてメンバーシップを更新する

Google Workspace 管理者は、update() メソッドを呼び出して、Google Workspace 組織内の任意のスペースのメンバーシップを更新できます。

Google Workspace 管理者としてこのメソッドを呼び出すには、次の操作を行います。

  • ユーザー認証を使用してメソッドを呼び出し、 管理者権限 でメソッドを呼び出すことをサポートする 認可スコープを指定します。
  • リクエストで、クエリ パラメータ useAdminAccesstrue に指定します。

詳細と例については、 Google Workspace 管理者として Google Chat スペースを管理するをご覧ください。