Allowlisted Domains API を設定する

このページでは、許可リストに登録されたドメインを追加一覧表示と取得、または削除する前に、許可リストに登録されたドメイン API を設定する方法について説明します。

始める前に

Cloud Identity API は、Cloud Identity を設定した後に使用する必要があります。手順については、Cloud Identity の設定をご覧ください。

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を初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $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 アクセスをドメイン全体の委任で制御するをご覧ください。ベスト プラクティスを確認して、ドメイン全体の委任の使用に関連するセキュリティ リスクを軽減します。

サービス アカウントを承認するには、次のスコープを指定します。

  • 読み取り / 書き込みオペレーションの場合createdeletelistget): https://www.googleapis.com/auth/cloud-identity.allowlisteddomains

  • 読み取り専用オペレーションの場合listget): 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 オペレーションを呼び出す詳細なサンプルコードについては、許可リストに登録されたドメインのリストを取得するをご覧ください。