허용 목록에 추가된 도메인 API 설정

이 페이지에서는 허용 목록에 추가된 도메인을 추가, 나열 및 가져오기 또는 삭제하기 전에 허용 목록에 추가된 도메인 API를 설정하는 방법을 설명합니다.

시작하기 전에

Cloud ID API를 사용하려면 먼저 Cloud ID를 설정해야 합니다. 안내를 보려면 Cloud ID 설정을 참고하세요.

Python 클라이언트 라이브러리 설치

Python 클라이언트 라이브러리를 설치하려면 다음 명령어를 실행합니다.

  pip install --upgrade google-api-python-client google-auth \
    google-auth-oauthlib google-auth-httplib2

Python 개발 환경 설정에 관한 자세한 내용은 Python 개발 환경 설정 가이드를 참조하세요.

API 사용 설정 및 사용자 인증 정보 설정

  1. 계정에 로그인합니다. Google Cloud 를 처음 사용하는 경우 Google Cloud, 계정을 만들어 실제 시나리오에서 제품이 어떻게 작동하는지 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Identity API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Identity API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  8. 서비스 계정을 만듭니다.
    • 서비스 계정 만들기 IAM 역할 (roles/iam.serviceAccountCreator) 및 프로젝트 IAM 관리자 역할 (roles/resourcemanager.projectIamAdmin)이 있는지 확인합니다. 역할을 부여하는 방법을 알아보세요.
    • 콘솔 Google Cloud 에서 서비스 계정 만들기 페이지로 이동합니다.
    • 프로젝트를 선택합니다.
    • 서비스 계정 이름 필드에 이름을 입력합니다. Google Cloud 콘솔은 이 이름을 기반으로 서비스 계정 ID 필드를 채웁니다.
    • 서비스 계정 설명 필드에 설명을 입력합니다. 예를 들면 Service account for quickstart입니다.
    • 만들고 계속하기 를 클릭합니다.
    • 서비스 계정에 프로젝트 > 소유자 역할을 부여합니다. 역할을 부여하려면 역할 선택 목록을 찾은 후 프로젝트 > 소유자 를 선택합니다.
    • 계속 을 클릭합니다.
    • 완료 를 클릭하여 서비스 계정 만들기를 마칩니다. 브라우저 창을 닫지 마세요. 다음 단계에서 사용합니다.
  9. 서비스 계정 키를 만듭니다.
    • 콘솔 Google Cloud 에서 만든 서비스 계정의 이메일 주소를 클릭합니다.
    • 를 클릭합니다.
    • 키 추가를 클릭한 후 새 키 만들기를 클릭합니다.
    • 만들기를 클릭합니다. JSON 키 파일이 컴퓨터에 다운로드됩니다.
    • 닫기 를 클릭합니다.

API 키 만들기

  1. Google Cloud 콘솔에서 사용자 인증 정보 페이지로 이동합니다.
  2. 사용자 인증 정보 만들기 를 클릭한 후 API 키 를 선택합니다.
  3. API 키 생성 완료 대화상자에 새로 만든 API 키가 표시됩니다. 이 키를 복사하여 스크립트에서 API_KEY 상수로 사용합니다.

도메인 전체 위임을 사용하여 서비스 계정으로 인증

허용 목록에 추가된 도메인을 관리하는 관리자이거나 관리자를 대신하여 허용 목록에 추가된 도메인을 관리할 수 있는 도메인 전체 권한을 계정에 제공하려면 서비스 계정으로 인증한 다음 서비스 계정에 도메인 전체 권한을 부여해야 합니다.

도메인 전체 위임 설정에 대한 자세한 내용은 도메인 전체 위임으로 API 액세스 제어를 참고하세요. 도메인 전체 위임 사용과 관련된 보안 위험을 완화하기 위한 권장사항을 검토하세요.

서비스 계정을 승인하려면 다음 범위를 제공하세요.

  • 읽기 및 쓰기 작업 (create, delete, list, get): https://www.googleapis.com/auth/cloud-identity.allowlisteddomains

  • 읽기 전용 작업 (list, get): https://www.googleapis.com/auth/cloud-identity.allowlisteddomains.readonly

사용자 인증 정보 초기화 및 클라이언트 인스턴스화

코드에서 사용자 인증 정보를 초기화할 때 사용자 인증 정보의 with_subject()를 호출하여 서비스 계정이 작동할 이메일 주소를 지정합니다.

다음 예시에서는 서비스 계정 사용자 인증 정보를 사용하여 클라이언트를 인스턴스화하여 허용 목록에 추가된 도메인 API와 상호작용하는 방법을 보여줍니다.

Python

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = [
    'https://www.googleapis.com/auth/cloud-identity.allowlisteddomains.readonly',
    'https://www.googleapis.com/auth/cloud-identity.allowlisteddomains',
]
SERVICE_ACCOUNT_FILE = 'SERVICE_ACCOUNT_CREDENTIAL_FILE'
PROD_DISCOVERY_URL_BASE = (
    'https://cloudidentity.googleapis.com/$discovery/rest'
)
API_KEY = 'YOUR_API_KEY'

def create_service(version, delegated_email):
  """Instantiates a client using service account credentials."""
  credentials = service_account.Credentials.from_service_account_file(
      SERVICE_ACCOUNT_FILE, scopes=SCOPES
  )
  delegated_credentials = credentials.with_subject(delegated_email)
  url = f'{PROD_DISCOVERY_URL_BASE}?version={version}&key={API_KEY}'

  service = googleapiclient.discovery.build(
      serviceName=None,
      version=None,
      credentials=delegated_credentials,
      discoveryServiceUrl=url,
      static_discovery=False,
  )
  return service

다음을 바꿉니다.

  • SERVICE_ACCOUNT_CREDENTIAL_FILE: 이 문서에서 이전에 만든 서비스 계정 키 파일
  • YOUR_API_KEY: Google Cloud 콘솔 사용자 인증 정보 페이지에서 복사한 API 키

허용 목록에 추가된 도메인 API 작업을 호출하는 자세한 샘플 코드는 허용 목록에 추가된 도메인 나열 및 가져오기에서 제공합니다.