Google Chat 스페이스에서 사용자의 멤버십 업데이트하기

이 가이드에서는 membership 리소스에서 patch 메서드를 사용하는 방법을 설명합니다. 멤버십에 대한 속성을 변경하기 위해 스페이스 멤버를 스페이스 관리자로 변경할 수도 있습니다.

Membership 리소스 사람 또는 Google Chat 앱이 초대되었는지 여부를 나타냅니다. 빈 공간을 사용할 수 있습니다.

기본 요건

Python

  • Python 3.6 이상
  • pip 패키지 관리 도구
  • 최신 Google 클라이언트 라이브러리 이러한 앱을 설치하거나 업데이트하려면 다음 단계를 따르세요. 명령줄 인터페이스에서 다음 명령어를 실행합니다.
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 이상
  • npm 패키지 관리 도구
  • 최신 Google 클라이언트 라이브러리 이러한 앱을 설치하거나 업데이트하려면 다음 단계를 따르세요. 명령줄 인터페이스에서 다음 명령어를 실행합니다.
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

멤버십 업데이트

스페이스 멤버십을 업데이트하려면 요청에 다음을 전달합니다.

  • chat.memberships 승인 범위를 지정합니다.
  • 먼저 patch 메서드 Membership 리소스에서 그런 다음 업데이트할 멤버십의 nameupdateMask를 전달합니다. 업데이트된 멤버십 속성을 지정하는 body입니다.
  • updateMask는 업데이트할 멤버십의 관점을 지정합니다. 다음이 포함됩니다.
    • role: Chat 스페이스 내에서의 사용자 역할로, 허용 여부를 결정합니다. 할 수 있습니다. 가능한 값은 다음과 같습니다.
      • ROLE_MEMBER: 스페이스의 멤버입니다. 사용자에게 기본 권한이 있습니다. 스페이스에 메시지를 보내는 것과 같은 기능을 예로 들 수 있습니다 1:1 및 이름이 지정되지 않은 그룹에서 모두에게 이 역할이 있습니다.
      • ROLE_MANAGER: 스페이스 관리자입니다. 사용자가 모든 기본 권한과 함께 스페이스를 관리할 수 있는 관리자 권한(예: 멤버 삭제 spaceType 필드가 SPACE인 스페이스에서만 지원됩니다. (이름이 지정된 스페이스)

일반 스페이스 멤버를 스페이스 관리자로 지정하기

다음 예에서는 다음을 지정하여 일반 스페이스 멤버를 스페이스 관리자로 지정합니다. body에서 업데이트된 멤버십을 지정하는 ROLE_MANAGER로서의 role 속성:

Python

  1. 작업 디렉터리에 chat_membership_update.py라는 파일을 만듭니다.
  2. chat_membership_update.py에 다음 코드를 포함합니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.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.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.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # 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. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름입니다. GCP 콘솔에서 spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.

    • MEMBERSHIP: 멤버십 이름으로, GCP 콘솔에서 spaces.members.list 메서드 채팅 API에서 지원됩니다.

  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_membership_update.py
    

Node.js

  1. 작업 디렉터리에 chat_membership_update.js라는 파일을 만듭니다.
  2. chat_membership_update.js에 다음 코드를 포함합니다.

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a membership in a Chat space to change it from
    * a space member to a space manager.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.spaces.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',
        requestBody: {
          role: 'ROLE_MANAGER'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    updateSpace().then(console.log);
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름입니다. GCP 콘솔에서 spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.

    • MEMBERSHIP: 멤버십 이름으로, GCP 콘솔에서 spaces.members.list 메서드 채팅 API에서 지원됩니다.

  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_membership_update.js
    

Apps Script

이 예시에서는 고급 채팅 서비스.

  1. chat.memberships 승인 범위를 Apps Script 프로젝트의 appsscript.json 파일:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. 이와 같은 함수를 Apps Script 프로젝트의 코드:

    /**
     * Updates a membership from space member to space manager.
     * @param {string} memberName The resource name of the membership.
    */
    function updateMembershipToSpaceManager(memberName) {
      try {
        const body = {'role': 'ROLE_MANAGER'};
        Chat.Spaces.Members.patch(memberName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }
    

Google Chat API가 지정된 멤버십을 스페이스 관리자로 변경하고 다음을 반환합니다. Membership의 인스턴스 자세히 설명하겠습니다.

스페이스 관리자를 정규 멤버로 설정하기

다음 예에서는 다음을 지정하여 스페이스 관리자를 일반 스페이스 멤버로 만듭니다. body에서 업데이트된 멤버십을 지정하는 ROLE_MEMBER로서의 role 속성:

Python

  1. 작업 디렉터리에 chat_membership_update.py라는 파일을 만듭니다.
  2. chat_membership_update.py에 다음 코드를 포함합니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.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.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.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # 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. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름입니다. GCP 콘솔에서 spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.

    • MEMBERSHIP: 멤버십 이름으로, GCP 콘솔에서 spaces.members.list 메서드 채팅 API에서 지원됩니다.

  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_membership_update.py
    

Node.js

  1. 작업 디렉터리에 chat_membership_update.js라는 파일을 만듭니다.
  2. chat_membership_update.js에 다음 코드를 포함합니다.

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a membership in a Chat space to change it from
    * a space manager to a space member.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.spaces.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',
        requestBody: {
          role: 'ROLE_MEMBER'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    updateSpace().then(console.log);
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름입니다. GCP 콘솔에서 spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.

    • MEMBERSHIP: 멤버십 이름으로, GCP 콘솔에서 spaces.members.list 메서드 채팅 API에서 지원됩니다.

  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_membership_update.js
    

Apps Script

이 예시에서는 고급 채팅 서비스.

  1. chat.memberships 승인 범위를 Apps Script 프로젝트의 appsscript.json 파일:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. 이와 같은 함수를 Apps Script 프로젝트의 코드:

    /**
     * Updates a membership from space manager to space member.
     * @param {string} memberName The resource name of the membership.
    */
    function updateMembershipToSpaceMember(memberName) {
      try {
        const body = {'role': 'ROLE_MEMBER'};
        Chat.Spaces.Members.patch(memberName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }
    

Google Chat API가 지정된 멤버십을 스페이스 관리자로 변경하고 다음을 반환합니다. Membership의 인스턴스 자세히 설명하겠습니다.