Google Cloud Search Query API では、ライセンスが付与されたドメイン ユーザーによる呼び出しの承認が必要です。サービス アカウントはライセンスが付与されたドメイン ユーザーではないため、デフォルトでは Query API を呼び出すことはできません。サービス アカウントが Query API 呼び出しを行えるようにするには、ドメイン管理者がドメイン全体の権限の委任を使用して、ドメインのユーザーデータへのアクセス権をサービス アカウントに付与します。 委任された権限を持つサービス アカウントは、Cloud Search にアクセスできる任意のユーザーに成り代わることができます。
サービス アカウントと認証情報を作成する
サービス アカウントの認証情報がない場合は、 サービス アカウントの認証情報を作成するをご覧ください。
ドメイン全体の権限をサービス アカウントに委任する
Google Workspace ドメインのユーザーデータにアクセスするには、ドメインの特権管理者がサービス アカウントにアクセス権を付与する必要があります。詳しくは、 ドメイン全体の委任を使用して Google Workspace API へのアクセスを制御するをご覧ください。
サービス アカウントにドメイン全体の権限を委任する手順は次のとおりです。
- ドメインの [Admin console]で、 [Main menu] > [Security] > [Access and data control] > [API controls]に移動します。
- [ドメイン全体の委任] ペインで、[ドメイン全体の委任を管理] を選択します。
- [新しく追加] をクリックします。
- [クライアント ID] フィールドに、サービス アカウントのクライアント ID を入力します。
- [OAuth スコープ] フィールドに、必要なスコープをカンマ区切りのリストで入力します。検索アプリケーションには
https://www.googleapis.com/auth/cloud_search.queryを使用します。 - [承認] をクリックします。
これで、サービス アカウントはドメイン全体で Cloud Search Query API にアクセスできるようになり、このスコープでドメインのユーザーに成り代わることができます。ドメインのユーザーに代わって、承認された Cloud Search API サービス オブジェクトをインスタンス化できるようになりました。
Cloud Search API サービス オブジェクトをインスタンス化する
このセクションでは、OAuth 2.0 とサービス アカウントの認証情報を使用して、Cloud Search API サービス オブジェクトをインスタンス化して承認する方法について説明します。次の例では、サービス アカウントの 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.
*
* @param userEmail The email of the user to impersonate.
* @return CloudSearch service object.
*/
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();
return new CloudSearch.Builder(httpTransport, jsonFactory, creds).build();
}
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 Cloud Search service object.
Args:
user_email: The email of the user to impersonate.
Returns:
Cloud Search Query API service object.
"""
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)