Google Cloud Search Query API yêu cầu các lệnh gọi phải được người dùng miền có giấy phép uỷ quyền. Vì tài khoản dịch vụ không phải là người dùng miền được cấp phép, nên theo mặc định, họ không thể gọi Query API. Để cho phép tài khoản dịch vụ thực hiện các lệnh gọi Query API, quản trị viên miền có thể sử dụng cơ chế uỷ quyền trên toàn miền để cấp cho tài khoản dịch vụ quyền truy cập vào dữ liệu người dùng của miền. Tài khoản dịch vụ có quyền uỷ quyền có thể mạo danh bất kỳ người dùng nào có quyền truy cập vào Cloud Search.
Tạo tài khoản dịch vụ và thông tin xác thực
Nếu bạn chưa có thông tin đăng nhập tài khoản dịch vụ, hãy xem phần Tạo thông tin đăng nhập tài khoản dịch vụ.
Uỷ quyền trên toàn miền cho tài khoản dịch vụ của bạn
Để truy cập vào dữ liệu người dùng trên một miền Google Workspace, quản trị viên cấp cao của miền đó phải cấp quyền truy cập cho tài khoản dịch vụ của bạn. Để biết thêm thông tin, hãy xem bài viết Kiểm soát quyền truy cập vào Google Workspace API bằng tính năng uỷ quyền trên toàn miền.
Cách uỷ quyền trên toàn miền cho một tài khoản dịch vụ:
- Trong Bảng điều khiển dành cho quản trị viên của miền, hãy chuyển đến phần Trình đơn chính > Bảo mật > Quyền truy cập và kiểm soát dữ liệu > Chế độ kiểm soát API.
- Trong ngăn Uỷ quyền trên toàn miền, hãy chọn Quản lý việc uỷ quyền trên toàn miền.
- Nhấp vào Thêm mới.
- Trong trường Mã ứng dụng khách, hãy nhập mã ứng dụng khách cho tài khoản dịch vụ của bạn.
- Trong trường Phạm vi OAuth, hãy nhập danh sách các phạm vi bắt buộc được phân tách bằng dấu phẩy. Sử dụng
https://www.googleapis.com/auth/cloud_search.querycho các ứng dụng tìm kiếm. - Nhấp vào Uỷ quyền.
Giờ đây, tài khoản dịch vụ của bạn có quyền truy cập trên toàn miền vào Cloud Search Query API và có thể mạo danh bất kỳ người dùng nào trong miền của bạn trong phạm vi này. Giờ đây, bạn có thể tạo một thực thể đối tượng dịch vụ Cloud Search API được uỷ quyền thay mặt cho người dùng trong miền của mình.
Tạo thực thể đối tượng dịch vụ Cloud Search API
Phần này trình bày cách khởi tạo và uỷ quyền cho một đối tượng dịch vụ Cloud Search API bằng OAuth 2.0 và thông tin đăng nhập tài khoản dịch vụ của bạn. Các ví dụ này đọc thông tin từ tệp khoá riêng tư JSON của tài khoản dịch vụ.
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)