Google Workspace ドメイン全体の権限の委任を実行する

Cloud Search Query API では、ドメイン内のライセンスを付与されたユーザーに属する OAuth 認証情報を使用して API 呼び出しを承認する必要があります。デフォルトでは、Indexing API と Configuration API へのアクセスに使用されるサービス アカウントは、Cloud Search または Google Workspace ライセンスを持つドメイン ユーザーではないため、クエリ API 呼び出しには使用できません。クエリ API 呼び出しの認証時にサービス アカウントを使用する場合、ドメイン管理者は、ドメイン全体のユーザーデータへのアクセス権をアカウントに付与できます。これは、ドメイン全体の権限の委任と呼ばれます。権限を委任されたサービス アカウントは、Cloud Search にアクセスできるユーザーを含む、すべてのユーザーになりすますことができます。

サービス アカウントと認証情報を作成する

サービス アカウントの認証情報がまだない場合は、サービス アカウントの認証情報を作成するをご覧ください。

ドメイン全体の権限をサービス アカウントに委任する

Google Workspace ドメインのユーザーデータにアクセスするには、作成したサービス アカウントに、ドメインの特権管理者によってアクセス権が付与されている必要があります。ドメイン全体の委任の詳細については、ドメイン全体の委任で Google Workspace API へのアクセスを制御するをご覧ください。

ドメイン全体の権限をサービス アカウントに委任するには:

  1. ドメインの管理コンソールから、メインメニュー > [セキュリティ] > [アクセスとデータ管理] > [API の制御] に移動します。
  2. [ドメイン全体の委任] ペインで、[ドメイン全体の委任を管理] を選択します。

  3. [新しく追加] をクリックします。

  4. [クライアント ID] フィールドに、上記のサービス アカウントの作成手順で取得したクライアント ID を入力します。

  5. [OAuth スコープ] フィールドに、アプリケーションに必要なスコープのカンマ区切りリストを入力します。Query API を使用する検索アプリケーションには、スコープ https://www.googleapis.com/auth/cloud_search.query を使用します。

  6. [承認] をクリックします。

これで、サービス アカウントはドメイン全体で Cloud Search Query API にアクセスできるようになり、このスコープ内でドメインのすべてのユーザーになりすますことができます。ドメインのユーザーに代わって、承認済みの Cloud Search API サービス オブジェクトをインスタンス化できるようになりました。

Cloud Search API サービス オブジェクトをインスタンス化する

このセクションでは、Cloud Search API サービス オブジェクトをインスタンス化し、OAuth 2.0 とサービス アカウントの認証情報を使用して API リクエストを行うことを承認し、Google Workspace ドメイン全体の委任を実行する方法について説明します。この例では、サービス アカウントの情報を JSON 形式の秘密鍵ファイルから読み取ります。

Java

import java.util.Collections;
import java.io.FileInputStream;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.cloudsearch.v1.CloudSearch;
import com.google.api.services.cloudsearch.v1.CloudSearchScopes;
...

/** Path to the Service Account's Private Key file */
private static final String SERVICE_ACCOUNT_FILE_PATH = "/path/to/key.json";

/**
 * Build and return a Cloud Search service object authorized with the service
 * account that acts on behalf of the given user.
 *
 * @param userEmail The email of the user to impersonate. Needs permissions to access Cloud Search.
 * @return CloudSearch service object that is ready to make requests.
 */
public static CloudSearch getCloudSearchAPIService(String userEmail)
    throws FileNotFoundException, IOException {

  FileInputStream credsFile = new FileInputStream(SERVICE_ACCOUNT_FILE_PATH);

  GoogleCredential init = GoogleCredential.fromStream(credsFile);

  HttpTransport httpTransport = init.getTransport();
  JsonFactory jsonFactory = init.getJsonFactory();

  GoogleCredential creds = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(init.getServiceAccountId())
      .setServiceAccountPrivateKey(init.getServiceAccountPrivateKey())
      .setServiceAccountScopes(Collections.singleton(CloudSearchScopes.CLOUD_SEARCH_QUERY))
      .setServiceAccountUser(userEmail)
      .build();

  CloudSearch service = new CloudSearch.Builder(httpTransport, jsonFactory, creds).build();

  return service;
}

Python

from google.oauth2 import service_account
from googleapiclient.discovery import build

# Path to the Service Account's Private Key file
SERVICE_ACCOUNT_FILE_PATH = "/path/to/key.json"

def create_query_api_service(user_email):
    """Build and return a CloudSearch service object authorized with the service
    account that acts on behalf of the given user.

    Args:
        user_email: The email of the user to impersonate. Needs permissions to access Cloud Search.
    Returns:
        Cloud Search Query API service object that is ready to make requests.
    """
    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE_PATH,
        scopes=['https://www.googleapis.com/auth/cloud_search.query'])

    delegated_credentials = credentials.with_subject(user_email)

    return build("cloudsearch", "v1", credentials=delegated_credentials)