Google Cloud Search Query API กำหนดให้การเรียก 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)