ดำเนินการมอบสิทธิ์ทั่วทั้งโดเมนของ Google Workspace

Cloud Search Query API กำหนดว่าการเรียก API ต้องได้รับอนุญาตโดยใช้ข้อมูลเข้าสู่ระบบ OAuth ที่เป็นของผู้ใช้ที่ได้รับอนุญาตในโดเมนของคุณ โดยค่าเริ่มต้นแล้ว บัญชีบริการที่ใช้เข้าถึง API สำหรับการจัดทำดัชนีและการกำหนดค่าจะใช้สำหรับการเรียก API การค้นหาไม่ได้ เนื่องจากไม่ใช่ผู้ใช้โดเมนที่มีใบอนุญาต Cloud Search หรือ Google Workspace หากคุณต้องการใช้บัญชีบริการเมื่อตรวจสอบสิทธิ์การเรียก API การค้นหา ผู้ดูแลระบบโดเมนสามารถให้สิทธิ์เข้าถึงข้อมูลผู้ใช้ทั่วทั้งโดเมนของบัญชี วิธีนี้เรียกว่าการมอบสิทธิ์ทั่วทั้งโดเมน บัญชีบริการที่มีหน่วยงานที่มอบสิทธิ์สามารถแอบอ้างเป็นผู้ใช้ใดก็ได้ รวมถึงผู้ใช้ที่มีสิทธิ์เข้าถึง Cloud Search

สร้างบัญชีบริการและข้อมูลเข้าสู่ระบบ

หากยังไม่มีข้อมูลเข้าสู่ระบบของบัญชีบริการ โปรดดูสร้างข้อมูลเข้าสู่ระบบของบัญชีบริการ

มอบสิทธิ์ทั่วทั้งโดเมนให้กับบัญชีบริการของคุณ

หากต้องการเข้าถึงข้อมูลผู้ใช้บนโดเมน Google Workspace บัญชีบริการที่คุณสร้างต้องมีสิทธิ์เข้าถึงโดยผู้ดูแลระบบขั้นสูงของโดเมน โปรดดูข้อมูลเพิ่มเติมเกี่ยวกับการมอบสิทธิ์ทั่วทั้งโดเมนที่หัวข้อควบคุมการเข้าถึง Google Workspace API ด้วยการมอบสิทธิ์ทั่วทั้งโดเมน

วิธีมอบสิทธิ์ทั่วทั้งโดเมนให้กับบัญชีบริการ

  1. จากคอนโซลผู้ดูแลระบบของโดเมน ให้ไปที่ เมนูหลัก > ความปลอดภัย > การเข้าถึงและการควบคุมข้อมูล > การควบคุม API
  2. ในแผงการมอบสิทธิ์ทั่วทั้งโดเมน ให้เลือกจัดการการมอบสิทธิ์ทั่วทั้งโดเมน

  3. คลิกเพิ่มใหม่

  4. ในช่องรหัสไคลเอ็นต์ ให้ป้อนรหัสไคลเอ็นต์ที่ได้รับจากขั้นตอนการสร้างบัญชีบริการข้างต้น

  5. ในช่องขอบเขต OAuth ให้ป้อนรายการขอบเขตที่คั่นด้วยคอมมาของขอบเขตที่จำเป็นสำหรับแอปพลิเคชัน ใช้ขอบเขต https://www.googleapis.com/auth/cloud_search.query สำหรับแอปพลิเคชันการค้นหาที่ใช้ API การค้นหา

  6. คลิกให้สิทธิ์

ตอนนี้บัญชีบริการของคุณมีสิทธิ์เข้าถึง Cloud Search Query API ทั่วทั้งโดเมน และแอบอ้างเป็นผู้ใช้ในโดเมนของคุณในขอบเขตนี้ได้ คุณพร้อมที่จะยืนยันออบเจ็กต์บริการ Cloud Search API ที่ได้รับอนุญาตในนามของผู้ใช้ในโดเมนแล้ว

สร้างอินสแตนซ์ออบเจ็กต์บริการ Cloud Search API

ส่วนนี้จะแสดงวิธีสร้างอินสแตนซ์ออบเจ็กต์บริการ Cloud Search API จากนั้นให้สิทธิ์เพื่อสร้างคำขอ API โดยใช้ OAuth 2.0 และข้อมูลเข้าสู่ระบบของบัญชีบริการเพื่อดำเนินการมอบสิทธิ์ทั่วทั้งโดเมน 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)