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 phải được uỷ quyền bằng thông tin đăng nhập OAuth thuộc về một người dùng được cấp phép trong miền của bạn. Theo mặc định, các tài khoản dịch vụ (được dùng để truy cập vào API lập chỉ mục và API cấu hình) không thể dùng cho các lệnh gọi API truy vấn vì đây 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, thì quản trị viên miền có thể cấp cho tài khoản quyền truy cập trên toàn miền vào dữ liệu người dùng. Đây được gọi là quyền 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, kể cả những 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 đăng nhập vào tài khoản dịch vụ, hãy tham khảo bài viết Tạo thông tin đăng nhập vào 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ấp quyền truy cập cho miền đó. Để biết thêm thông tin về tính năng 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 vào 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. Trên 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 > Quyền kiểm soát API.
  2. 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.

  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 mà bạn nhận đượ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à ứng dụng của bạn yêu cầu. Sử dụng phạm vi https://www.googleapis.com/auth/cloud_search.query cho các ứng dụng tìm kiếm bằng Query API.

  6. 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. Bạn đã sẵn sàng tạo một đố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 bạn.

Tạo một đối tượng dịch vụ Cloud Search API

Phần này cho biết cách khởi tạo một đối tượng dịch vụ Cloud Search API, sau đó uỷ quyền đối tượng này thực hiện các yêu cầu API bằng OAuth 2.0 và thông tin đăng nhập của tài khoản dịch vụ để thực hiện việc uỷ quyền trên toàn miền 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)