Thực hiện việc uỷ quyền trên toàn miền của Google Workspace

Cloud Search Query API yêu cầu các lệnh gọi API được uỷ quyền bằng thông tin đăng nhập OAuth của người dùng được cấp phép trong miền của bạn. Theo mặc định, bạn không thể dùng tài khoản dịch vụ (dùng để truy cập vào các API cấu hình và lập chỉ mục) cho các lệnh gọi API truy vấn vì họ không phải là người dùng miền có giấy phép Cloud Search hoặc Google Workspace. Nếu bạn muốn sử dụng tài khoản dịch vụ khi xác thực các lệnh gọi API truy vấn, quản trị viên miền có thể cấp cho tài khoản quyền truy cập vào dữ liệu người dùng trên toàn miền – đây được gọi là uỷ quyền trên toàn miền. Tài khoản dịch vụ được uỷ quyền có thể mạo danh bất kỳ người dùng nào, bao gồm cả người dùng 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 xác thực tài khoản dịch vụ, hãy tham khảo bài viết Tạo thông tin xác thực 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, tài khoản dịch vụ mà bạn đã tạo cần được quản trị viên cấp cao của miền cấp quyền truy cập. Để biết thêm thông tin về việc uỷ quyền trên toàn miền, hãy xem bài viết Kiểm soát quyền truy cập của API Google Workspace 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ụ:

  1. Trong Bảng điều khiển dành cho quản trị viên của miền, hãy chuyển đến Trình đơn chính > Bảo mật > Kiểm soát quyền truy cập và dữ liệu > Các tùy chọn kiểm soát API.
  2. Trong ngăn Uỷ quyền trên toàn miền, hãy chọn Manage Domains Delegation (Quản lý uỷ quyền trên toàn miền).

  3. Nhấp vào Thêm mới.

  4. Trong trường Mã ứng dụng khách, hãy nhập mã ứng dụng khách thu được từ các bước tạo tài khoản dịch vụ ở trên.

  5. Trong trường Phạm vi OAuth, hãy nhập danh sách các phạm vi được phân tách bằng dấu phẩy mà bạn cần dùng cho ứng dụng. Sử dụng phạm vi https://www.googleapis.com/auth/cloud_search.query cho các ứng dụng tìm kiếm sử dụng API Truy vấn.

  6. Nhấp vào Uỷ quyền.

Tài khoản dịch vụ của bạn hiệ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. Bạn đã sẵn sàng thay mặt người dùng trong miền của mình tạo thực thể cho một đối tượng dịch vụ Cloud Search API được uỷ quyền.

Tạo thực thể cho đối tượng dịch vụ Cloud Search API

Phần này cho biết cách tạo thực thể cho một đối tượng dịch vụ API Cloud Search rồi uỷ quyền cho đối tượng đó thực hiện các yêu cầu API bằng OAuth 2.0 và thông tin xác thực của tài khoản dịch vụ để uỷ quyền trên toàn miền của Google Workspace. Các ví dụ này đọc thông tin của tài khoản dịch vụ từ tệp khoá riêng tư có định dạng 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)