스페이스 나열

이 가이드에서는 다음의 Space 리소스에서 list 리소스를 사용하는 방법을 설명합니다. Google Chat API를 사용하여 스페이스를 나열합니다. 스페이스를 나열하면 페이지로 나눈 필터링이 가능한 스페이스의 목록입니다.

Space 리소스 는 사람과 채팅 앱이 메시지를 보낼 수 있는 위치를 나타냅니다. 파일 공유, 공동작업 등이 가능합니다 다음과 같은 여러 유형의 스페이스가 있습니다.

  • 채팅 메시지 (DM)는 사용자 2명 또는 상대방과 채팅 앱
  • 그룹 채팅은 세 명 이상의 사용자와 채팅 앱
  • 이름이 지정된 스페이스는 사용자가 메시지를 보내고, 파일을 공유하고, 공동작업도 가능합니다

다음 항목이 있는 스페이스 나열 앱 인증 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
    

사용자 인증이 있는 스페이스 나열

Google Chat에서 스페이스를 나열하려면 다음을 요청:

다음 예는 이름이 지정된 스페이스와 그룹 채팅 (직접 제외)을 나열합니다. 메일(필터링된 메일)은 인증된 사용자에게 표시됩니다.

Python

  1. 작업 디렉터리에 chat_space_list.py라는 파일을 만듭니다.
  2. chat_space_list.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.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists named spaces and group chats (but not direct messages)
        visible to the authenticated user.
        '''
    
        # 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().list(
    
              # An optional filter that returns named spaces or unnamed group chats,
              # but not direct messages (DMs).
              filter='spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
    
          ).execute()
    
        # Prints the returned list of spaces.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_space_list.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * List Chat spaces.
    * @return {!Promise<!Object>}
    */
    async function listSpaces() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.list({
        filter: 'spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
      });
    }
    
    listSpaces().then(console.log);
    
  3. 작업 디렉터리에서 샘플을 실행합니다.

    node list-spaces.js
    

Chat API는 이름이 지정된 스페이스 및 그룹 채팅의 페이지로 나눈 배열

앱 인증으로 스페이스 나열

Google Chat에서 스페이스를 나열하려면 다음을 요청:

다음 예는 이름이 지정된 스페이스와 그룹 채팅 (직접 제외)을 나열합니다. 메시지)가 표시됩니다.

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().list(
    
            # An optional filter that returns named spaces or unnamed
            # group chats, but not direct messages (DMs).
            filter='spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
    
        ).execute()
    
    print(result)
    
  3. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_space_list_app.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    
    /**
    * List Chat spaces.
    * @return {!Promise<!Object>}
    */
    async function listSpaces() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.list({
        filter: 'spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
      });
    }
    
    listSpaces().then(console.log);
    
  3. 작업 디렉터리에서 샘플을 실행합니다.

    node app-list-spaces.js
    

Chat API는 페이지로 나눈 스페이스 배열을 반환합니다.

페이지로 나누기 맞춤설정 또는 목록 필터링

Google Chat에서 스페이스를 나열하려면 다음 선택적 매개변수를 전달합니다. 쿼리 매개변수를 사용하여 나열된 스페이스의 페이지로 나누기를 맞춤설정하거나 스페이스를 필터링할 수 있습니다.

  • pageSize: 반환할 최대 공백 수입니다. 서비스가 이 값보다 작을 수 있습니다. 지정하지 않으면 최대 100개의 공백이 반환됩니다. 이 최댓값은 1,000이고 1,000을 초과하는 값은 자동으로 1,000명입니다.
  • pageToken: 이전 스페이스 목록 호출에서 수신된 페이지 토큰입니다. 후속 페이지를 검색하려면 이 토큰을 제공하세요. 페이지로 나누기 시 필터 값이 페이지 토큰을 제공한 호출과 일치해야 합니다. 예기치 않은 결과가 발생할 수 있습니다
  • filter: 쿼리 필터입니다. 지원되는 쿼리 세부정보는 다음을 참조하세요. spaces.list 메서드