ดำเนินการมอบสิทธิ์ทั่วทั้งโดเมนของ 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 สำหรับแอปพลิเคชันการค้นหาโดยใช้ 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)