執行 Google Workspace 全網域授權委派

Cloud Search Query API 規定 API 呼叫的授權方法是使用您網域中授權使用者的 OAuth 憑證。根據預設,用來存取索引和設定 API 的服務帳戶無法用於查詢 API 呼叫,因為這些帳戶並非具備 Cloud Search 或 Google Workspace 授權的網域使用者。如果您想在驗證查詢 API 呼叫時使用服務帳戶,網域管理員可以將使用者資料的全網域存取權授予帳戶,這稱為「全網域授權委派」。具有委派授權的服務帳戶可以模擬任何使用者,包括具有 Cloud Search 存取權的使用者。

建立服務帳戶和憑證

如果您沒有服務帳戶憑證,請參閱「建立服務帳戶憑證」一文。

將全網域授權委派給服務帳戶

如要存取 Google Workspace 網域中的使用者資料,您建立的服務帳戶需要由網域的超級管理員授予存取權。如要進一步瞭解全網域委派,請參閱「使用全網域委派功能控管 Google Workspace API 存取權」。

如何將全網域授權委派給服務帳戶:

  1. 在網域的管理控制台中,依序前往「主選單」 >「安全性」 >「存取權與資料控管」 >「API 控制項」
  2. 在「全網域委派」窗格中,選取「管理全網域委派」

  3. 按一下 [Add new] (新增)

  4. 在「Client ID」(用戶端 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)