執行 Google Workspace 全網域授權委派

Cloud Search Query API 規定 API 呼叫必須以 OAuth 授權 擁有網域中授權使用者的憑證。根據預設,Service 存取索引和設定 API,則無法 用於查詢 API 呼叫,因為他們並非 Cloud Search 中的網域使用者 或 Google Workspace 授權如要使用服務帳戶 驗證查詢 API 呼叫,網域管理員可授予帳戶權限 全網域存取使用者資料 (稱為 全網域授權委派。已委派的服務帳戶 機構可以冒用任何使用者的身分,包括具備 Cloud Search 存取權的使用者。

建立服務帳戶和憑證

如果您沒有服務帳戶憑證,請參閱 建立服務帳戶憑證

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

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

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

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

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

  4. 在 [用戶端 ID] 欄位中,輸入 服務帳戶建立步驟

  5. 在「OAuth 範圍」欄位中輸入範圍清單,並以半形逗號分隔 應用程式所需的憑證使用範圍 針對搜尋應用程式使用 https://www.googleapis.com/auth/cloud_search.query 來檢視模型

  6. 按一下「授權」

您的服務帳戶現在可以存取 Cloud Search Query API 的全網域存取權。 並可以冒用您網域中任何使用者的身分。可以開始使用了 以下列方式將已授權的 Cloud Search API 服務物件執行個體化: 網域使用者

將 Cloud Search API 服務物件例項化

本節說明如何將 Cloud Search API 服務物件例項化,然後 授權 API 使用 OAuth 2.0 和服務帳戶的 執行 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)