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

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