Google Chat スペースでユーザーのメンバーシップを更新する

このガイドでは、Google Chat API の Membership リソースで update() メソッドを使用して、メンバーシップの属性を変更する方法について説明します。たとえば、スペースのメンバーをスペースの管理者に変更したり、スペースの管理者をスペースのメンバーに変更したりできます。

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

Membership リソースは、ユーザーまたは Google Chat アプリがスペースに招待されているか、スペースに参加しているか、スペースに参加していないかを表します。

前提条件

Node.js

メンバーシップを更新する

スペース メンバーシップを更新するには、リクエストで次の情報を渡します。

  • 認可スコープを指定します。
    • ユーザー認証で、chat.memberships 認可スコープを指定します。
    • アプリ認証では、chat.app.memberships 認証スコープを指定します。アプリ認証を使用してメンバーシップを更新する場合、更新できるのは Chat 用アプリによって作成されたスペースのメンバーシップのみです。アプリの認証には、1 回限りの管理者による承認が必要です。
  • UpdateMembership() メソッドを呼び出します。
  • 次のコードを使用して、membershipMembership のインスタンスとして渡します。
    • 更新するメンバーシップに設定された name フィールド。スペース ID とメンバー ID が含まれます。
    • 更新するメンバーシップ フィールドが新しい値に設定されます。
  • updateMask を渡して、更新するメンバーシップの側面を指定します。これには次のものが含まれます。
    • role: Chat スペース内のユーザーのロール。スペース内で許可される操作が決まります。指定できる値は次のとおりです。
      • ROLE_MEMBER: スペースのメンバー。ユーザーには、スペースにメッセージを送信するなどの基本的な権限があります。1 対 1 の会話と名前のないグループの会話では、全員にこの役割が割り当てられます。
      • ROLE_MANAGER: スペースの管理者。ユーザーは、すべての基本権限に加えて、メンバーの追加や削除など、スペースを管理できる管理権限を持っています。spaceTypeSPACE(名前付きスペース)のスペースでのみサポートされます。

ユーザーとしてスペースの通常のメンバーをスペースの管理者にする

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

main().catch(console.error);

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

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

Google Chat API は、指定されたメンバーシップをスペース管理者に更新し、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);
}

main().catch(console.error);

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

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

Google Chat API は、指定されたメンバーシップをスペース管理者に更新し、Membership のインスタンスを返します。

Chat 用アプリとして通常のスペース メンバーをスペースの管理者にする

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

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

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

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)
    
        # 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. コードで、次のように置き換えます。

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

    python3 chat_membership_update_to_manager_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 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)
    
        # 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 スペースを管理するをご覧ください。