執行 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)