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

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