执行 Google Workspace 全网域授权

Cloud Search Query API 要求使用属于您网域中已获许可用户的 OAuth 凭据授权 API 调用。默认情况下,用于访问索引编制和配置 API 的服务帐号不能用于查询 API 调用,因为它们不是拥有 Cloud Search 或 Google Workspace 许可的网域用户。如果您希望在对查询 API 调用进行身份验证时使用服务帐号,则网域管理员可以授予该帐号对用户数据的全网域访问权限,这称为“全网域授权委托”。拥有委托权限的服务帐号可以模拟任何用户,包括有权访问 Cloud Search 的用户。

创建服务账号和凭据

如果您还没有服务帐号凭据,请参阅创建服务帐号凭据

向您的服务账号进行全网域授权

如需访问 Google Workspace 网域中的用户数据,您创建的服务帐号需要由该网域的超级用户授予访问权限。如需详细了解全网域授权,请参阅使用全网域授权功能控制 Google Workspace API 访问权限

如需将全网域授权委派给服务帐号,请执行以下操作:

  1. 在网域的管理控制台中,转到主菜单 > 安全性 > 访问权限和数据控制 > API 控件
  2. 全网域授权窗格中,选择管理全网域授权

  3. 点击新增

  4. 客户端 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)