执行 Google Workspace 全网域授权

Google Cloud Search Query API 要求调用由已获许可的网域用户授权。由于服务账号不是已获许可的网域用户,因此默认情况下无法调用 Query API。如需让服务账号能够调用 Query API,网域管理员可以使用 全网域授权功能,授予服务账号对网域用户数据的访问权限。获得此授权的服务账号可以模拟任何有权访问 Cloud Search 的用户。

创建服务账号和凭据

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

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

如需访问 Google Workspace 网域中的用户数据,该网域的超级用户必须向您的服务账号授予访问权限。如需了解详情, 请参阅 使用全网域授权功能控制 Google Workspace API 访问权限

如需向服务账号进行全网域授权,请执行以下操作:

  1. 在您网域的管理控制台中,依次前往 主菜单 > 安全性 > 访问权限和数据控件 > API 控件
  2. 全网域授权 窗格中,选择管理全网域授权
  3. 点击新增
  4. 客户端 ID 字段中,输入服务账号的客户端 ID。
  5. OAuth 范围 字段中,输入所需的逗号分隔列表范围。对于搜索应用,请使用 https://www.googleapis.com/auth/cloud_search.query
  6. 点击授权

这样,您的服务账号即可获得对 Cloud Search Query API 的全网域访问权限,并且可以在该作用域内模拟您的网域的任意用户。您现在可以代表您网域的用户实例化授权的 Cloud Search API 服务对象。

对 Cloud Search API 服务对象进行实例化

本部分介绍了如何使用 OAuth 2.0 和您的服务账号凭据实例化 Cloud Search API 服务对象并对其进行授权。这些示例从服务账号的 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.
 *
 * @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)