스페이스 세부정보 보기

이 가이드에서는 Google Chat API의 Space 리소스에서 get 메서드를 사용하여 표시 이름, 설명, 가이드라인과 같은 스페이스에 관한 세부정보를 확인하는 방법을 설명합니다.

Space 리소스는 사용자와 채팅 앱이 메시지를 전송하고 파일을 공유하며 공동작업할 수 있는 위치를 나타냅니다. 스페이스에는 다음과 같은 여러 유형이 있습니다.

  • 채팅 메시지 (DM)는 두 사용자 또는 사용자와 채팅 앱 간의 대화입니다.
  • 그룹 채팅은 3명 이상의 사용자와 채팅 앱 간의 대화입니다.
  • 이름이 지정된 스페이스는 사용자가 메시지를 전송하고 파일을 공유하며 공동작업할 수 있는 영구적인 장소입니다.

앱 인증으로 인증하면 채팅 앱이 Google Chat에서 액세스할 수 있는 스페이스 (예: 앱이 속한 스페이스)를 채팅 앱이 가져올 수 있습니다. 사용자 인증으로 인증하면 인증된 사용자가 액세스할 수 있는 스페이스를 가져올 수 있습니다.

기본 요건

Python

  • Python 3.6 이상
  • pip 패키지 관리 도구
  • Python용 최신 Google 클라이언트 라이브러리입니다. 설치하거나 업데이트하려면 명령줄 인터페이스에서 다음 명령어를 실행합니다.

    pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
    
  • 게시된 채팅 앱. 채팅 앱을 만들고 게시하려면 Google Chat 앱 빌드를 참조하세요.

  • 채팅 앱에 구성된 승인. 스페이스 가져오기는 다음 두 가지 인증 방법을 모두 지원합니다.

    • chat.spaces.readonly 또는 chat.spaces 승인 범위를 사용한 사용자 인증
    • chat.bot 승인 범위를 사용한 앱 인증

스페이스 구매

Google Chat에서 스페이스를 가져오려면 요청에 다음을 전달합니다.

  • 앱 인증에서 chat.bot 승인 범위를 지정합니다. 사용자 인증에서 chat.spaces.readonly 또는 chat.spaces 승인 범위를 지정합니다.
  • Space 리소스에서 get 메서드를 호출하여 가져올 공간의 name를 전달합니다. Google Chat의 스페이스 리소스 또는 스페이스 URL에서 스페이스 이름을 가져옵니다.

사용자 인증으로 스페이스 세부정보 가져오기

사용자 인증을 통해 스페이스 세부정보를 가져오는 방법은 다음과 같습니다.

Python

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

    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # 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.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets details about a specified space.
        '''
    
        # 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().get(
    
              # The space to get.
              #
              # Replace SPACE with a space name.
              # Obtain the space name from the spaces resource of Chat API,
              # or from a space's URL.
              name='spaces/SPACE'
    
          ).execute()
    
        # Prints details about the space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 SPACE을 스페이스 이름으로 바꿉니다. 이 이름은 Chat API의 spaces.list 메서드 또는 스페이스의 URL에서 가져올 수 있습니다.

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

    python3 chat_space_get_user.py
    

Chat API는 지정된 스페이스를 자세히 설명하는 Space의 인스턴스를 반환합니다.

앱 인증으로 스페이스 세부정보 받기

앱 인증을 사용하여 공간 세부정보를 가져오는 방법은 다음과 같습니다.

Python

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

    from httplib2 import Http
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http()))
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().get(
    
        # The space to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        name='spaces/SPACE'
    
    ).execute()
    
    print(result)
    
  3. 코드에서 SPACE을 스페이스 이름으로 바꿉니다. 이 이름은 Chat API의 spaces.list() 메서드 또는 스페이스의 URL에서 가져올 수 있습니다.

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

    python3 chat_space_get_app.py
    

Chat API는 지정된 스페이스를 자세히 설명하는 Space 인스턴스를 반환합니다.