사용자 초대 API 설정

이 페이지에서는 Cloud ID 사용자 초대 API를 설정하는 방법을 설명합니다.

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

  1. 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

  2. Enable the Cloud Identity API, if it is not already enabled.

    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

  3. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the Service Account Token Creator role to the service account.

      To grant the role, find the Select a role list, then select Service Account Token Creator.

    7. Click Continue.
    8. Click Done to finish creating the service account.

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

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

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

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

클라이언트 인증 및 인스턴스화

User Invitation API를 서비스 계정으로 호출하려면 도메인 전체 위임 (DWD)을 사용해야 합니다. 애플리케이션 기본 사용자 인증 정보 (ADC) 또는 서비스 계정 키를 사용하여 서비스 계정을 인증할 수 있습니다.

DWD 없이 관리자로 인증하려면 https://www.googleapis.com/auth/cloud-identity.userinvitations 범위가 있는 최종 사용자 OAuth 2.0 사용자 인증 정보를 대신 사용합니다. 자세한 내용은 웹 서버 애플리케이션용 OAuth 2.0 사용을 참조하세요.

도메인 전체 위임 설정

도메인 전체 위임을 사용하려면 서비스 계정에 관리자를 가장할 수 있는 권한을 부여해야 합니다. 감사 로그에는 가장된 사용자로 수행된 서비스 계정 작업이 표시됩니다

자세한 내용은 서비스 계정에 도메인 전체 권한 위임을 참조하세요.

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

  • https://www.googleapis.com/auth/cloud-identity.userinvitations

클라이언트 인스턴스화

클라이언트를 인스턴스화하려면 인증 방법을 선택합니다. 코드가Google Cloud 에서 실행되거나 워크로드 아이덴티티 제휴를 사용하는 경우 애플리케이션 기본 사용자 인증 정보 (ADC)를 사용하는 것이 좋습니다.

ADC

from google.auth import iam
from google.auth.transport import requests
import google.auth
from google.oauth2 import service_account

INITIAL_SCOPES = ['https://www.googleapis.com/auth/iam']
CI_SCOPES = ['https://www.googleapis.com/auth/cloud-identity.userinvitations']
TOKEN_URI = "https://accounts.google.com/o/oauth2/token"

def create_delegated_credentials(
  user_email: str
) -> google.auth.credentials.Credentials:
  """Creates delegated credentials for the user.

  Args:
    user_email: The administrator email to call as.

  Returns:
    The delegated credentials for the user.
  """

  # Fetch application default credentials (ADC)
  credentials, _ = google.auth.default(scopes=INITIAL_SCOPES)

  # Populate account information
  request = requests.Request()
  credentials.refresh(request)

  # Create an IAM signer
  signer = iam.Signer(request, credentials,
                      credentials.service_account_email)

  # Create domain-wide delegated (DWD) credentials
  delegated_credentials = service_account.Credentials(
      signer=signer,
      service_account_email=credentials.service_account_email,
      token_uri=TOKEN_URI,
      scopes=CI_SCOPES,
      subject=user_email
  )

  return delegated_credentials

서비스 계정 키

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/cloud-identity.userinvitations']
SERVICE_ACCOUNT_FILE = '/path/to/service-account-file.json'

def create_service():
  credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
  delegated_credentials = credentials.with_subject('user@altostrat.com')

  service_name = 'cloudidentity'
  api_version = 'v1'
  service = googleapiclient.discovery.build(
    service_name,
    api_version,
    credentials=delegated_credentials)

  return service

이 코드를 실행한 후 User Invitation API를 호출할 수 있습니다.

다음 단계