User Invitation API を設定する

このページでは、Cloud Identity User Invite 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 で実行されている場合、または Workload Identity 連携を使用している場合は、アプリケーションのデフォルト認証情報(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 の呼び出しを開始できます。

次のステップ