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

Cloud Search Query API では、API 呼び出しを OAuth を使用して承認する必要があります ドメイン内でライセンスが付与されたユーザーの認証情報です。デフォルトでは、 インデックスと構成の API へのアクセスに使用される、 Cloud Search のドメイン ユーザーではないため、クエリ API 呼び出しに使用されている Google Workspace ライセンスが必要ですサービス アカウントを Google Cloud の 場合、ドメイン管理者はそのアカウントに対して ユーザーデータへのアクセスをドメイン全体で許可できます。これは ドメイン全体の権限の委任。サービス アカウントは、Compute Engine の 権限は、Cloud Search にアクセスできるユーザーを含め、あらゆるユーザーになりすますことができます。

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

サービス アカウントの認証情報をまだ取得していない場合は、 サービス アカウントの認証情報を作成します

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

Google Workspace ドメインのユーザーデータにアクセスするには、 ドメインの特権管理者からアクセス権を付与してもらう必要があります。 ドメイン全体の委任について詳しくは、このモジュールの ドメイン全体の委任で Google Workspace API へのアクセスを制御する

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

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

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

  4. [クライアント ID] 欄に、 サービス アカウントの作成手順は前述のとおりです。

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

  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)