Google Cloud Search Query API กำหนดให้การเรียกใช้ต้องได้รับสิทธิ์จากผู้ใช้โดเมนที่มีใบอนุญาต เนื่องจากบัญชีบริการไม่ใช่ผู้ใช้โดเมนที่ได้รับอนุญาต จึงเรียกใช้ Query API ไม่ได้โดยค่าเริ่มต้น หากต้องการเปิดใช้บัญชีบริการเพื่อทำการเรียกใช้ Query API ผู้ดูแลระบบโดเมนสามารถใช้การมอบสิทธิ์ทั่วทั้งโดเมนเพื่อมอบสิทธิ์เข้าถึงข้อมูลผู้ใช้ของโดเมนให้แก่บัญชีบริการได้ บัญชีบริการที่มีสิทธิ์ที่ได้รับมอบหมายสามารถแอบอ้างเป็นผู้ใช้ที่มี สิทธิ์เข้าถึง Cloud Search ได้
สร้างบัญชีบริการและข้อมูลเข้าสู่ระบบ
หากไม่มีข้อมูลเข้าสู่ระบบของบัญชีบริการ โปรดดูสร้างข้อมูลเข้าสู่ระบบของบัญชีบริการ
มอบสิทธิ์ระดับโดเมนให้กับบัญชีบริการ
หากต้องการเข้าถึงข้อมูลผู้ใช้ในโดเมน Google Workspace ผู้ดูแลระบบขั้นสูง ของโดเมนต้องให้สิทธิ์เข้าถึงบัญชีบริการของคุณ ดูข้อมูลเพิ่มเติมได้ที่ควบคุมการเข้าถึง Google Workspace API ด้วยการมอบสิทธิ์ทั่วทั้งโดเมน
วิธีมอบสิทธิ์ทั่วทั้งโดเมนให้กับบัญชีบริการ
- ในคอนโซลผู้ดูแลระบบของโดเมน ให้ไปที่ เมนูหลัก > ความปลอดภัย > การเข้าถึงและการควบคุมข้อมูล > การควบคุม API
- ในบานหน้าต่างการมอบสิทธิ์ทั่วทั้งโดเมน ให้เลือก จัดการการมอบสิทธิ์ทั่วทั้งโดเมน
- คลิกเพิ่มใหม่
- ในช่องรหัสไคลเอ็นต์ ให้ป้อนรหัสไคลเอ็นต์ของบัญชีบริการ
- ในช่องขอบเขต OAuth ให้ป้อนรายการขอบเขตที่จำเป็นซึ่งคั่นด้วยคอมมา
ใช้
https://www.googleapis.com/auth/cloud_search.queryสำหรับแอปพลิเคชันค้นหา - คลิกให้สิทธิ์
ตอนนี้บัญชีบริการของคุณมีสิทธิ์เข้าถึง Cloud Search Query API ทั่วทั้งโดเมนแล้ว และสามารถแอบอ้างเป็นผู้ใช้ในโดเมนของคุณได้ภายในขอบเขตนี้ ตอนนี้คุณสามารถสร้างออบเจ็กต์บริการ Cloud Search API ที่ได้รับอนุญาตในนามของผู้ใช้ในโดเมนได้แล้ว
สร้างออบเจ็กต์บริการ Cloud Search API
ส่วนนี้แสดงวิธีกำหนดอินสแตนซ์และให้สิทธิ์ออบเจ็กต์บริการ Cloud Search API โดยใช้ OAuth 2.0 และข้อมูลเข้าสู่ระบบบัญชีบริการ ตัวอย่างเหล่านี้อ่านข้อมูลจากไฟล์คีย์ส่วนตัว 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)