Cloud Search Query API 規定,API 呼叫必須使用您網域中授權使用者的 OAuth 憑證授權。根據預設,服務帳戶 (用於存取索引和設定 API) 無法用於查詢 API 呼叫,因為這些帳戶不是具備 Cloud Search 或 Google Workspace 授權的網域使用者。如要在驗證查詢 API 呼叫時使用服務帳戶,網域管理員可以授予該帳戶全網域使用者資料存取權,這稱為全網域授權委派。具有委派授權的服務帳戶可以模擬任何使用者,包括有權存取 Cloud Search 的使用者。
建立服務帳戶和憑證
如果您還沒有服務帳戶憑證,請參閱「建立服務帳戶憑證」。
將全網域授權委派給服務帳戶
如要存取 Google Workspace 網域中的使用者資料,您建立的服務帳戶必須獲得網域超級管理員的存取權。如要進一步瞭解全網域委派功能,請參閱「使用全網域委派功能控管 Google Workspace API 存取權」。
如何將網域層級的權限委派給服務帳戶:
- 在網域的管理控制台中,依序前往「主選單」 >「安全性」 >「存取權與資料控管」 >「API 控制項」。
在「全網域委派」窗格中,選取「管理全網域委派設定」。
點選「新增」。
在「用戶端 ID」欄位中,輸入從上述服務帳戶建立步驟取得的用戶端 ID。
在「OAuth 範圍」欄位中,輸入應用程式所需的範圍清單,並以半形逗號分隔。使用 Query API 時,請為搜尋應用程式使用
https://www.googleapis.com/auth/cloud_search.query
範圍。點選「授權」。
您的服務帳戶現在已取得 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)