設定 Enterprise Knowledge Graph API

本指南提供開始使用 Enterprise Knowledge Graph 的所有必要設定步驟。

關於 Google Cloud 控制台

Google Cloud console 是網頁版 UI,可用於佈建、設定、管理和監控使用 Google Cloud 產品的系統。您可以使用 Google Cloud 控制台設定及管理 Enterprise Knowledge Graph 資源。

建立專案

如要使用 Google Cloud提供的服務,必須建立專案

專案可整理您所有 Google Cloud 資源。專案包含下列元件:

  • 一組協作者
  • 已啟用的 API (和其他資源)
  • 監控工具
  • 帳單資訊
  • 驗證和存取權控管

您可以建立一個或多個專案。您可以使用專案,在資源階層中整理 Google Cloud 資源。如要進一步瞭解專案,請參閱 Resource Manager 說明文件

In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

Roles required to select or create a project

  • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
  • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

Go to project selector

啟用 API

您必須為專案啟用 Enterprise Knowledge Graph API。如要進一步瞭解如何啟用 API,請參閱「服務使用情形說明文件」。

Enable the Enterprise Knowledge Graph API.

Roles required to enable APIs

To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

Enable the API

設定驗證方法

任何使用該 API 的用戶端應用程式都必須經過驗證,並獲得所要求資源的存取權。本節說明重要的驗證概念,並提供其設定步驟。詳情請參閱「驗證總覽」。

關於服務帳戶

驗證方法有多種選項,但是建議您使用服務帳戶來進行驗證和存取權控管。這是因為服務帳戶可以用來提供應用程式憑證,而使用者憑證則無法達到相同目的。專案擁有自己的服務帳戶。您可以為一個專案建立許多服務帳戶。詳情請參閱服務帳戶

關於角色

呼叫 API 時, Google Cloud 會要求呼叫身分 (任何適用人員、實體或程序及其定義的屬性) 具備適當的權限。您可以將角色授予服務帳戶,藉此授予權限。詳情請參閱「身分與存取權管理 (IAM) 說明文件」。

為了嘗試使用 Enterprise Knowledge Graph API,您可以在下列步驟中使用專案 > 擁有者角色。「專案 > 擁有者」角色會授予服務帳戶專案中資源的完整權限。如果服務帳戶不需要完整權限,您可以使用 Google Cloud 控制台指定限制較多的角色。如需 Enterprise Knowledge Graph 的權限和角色清單,請參閱「Enterprise Knowledge Graph 權限」和「Enterprise Knowledge Graph 角色」。如要瞭解如何使用 IAM 角色管理權限,請參閱「將角色授予服務帳戶」一文。

關於服務帳戶金鑰

服務帳戶與一或多個公開或私密金鑰組相關聯。建立新的金鑰組時,您將下載私密金鑰。Google Cloud CLI 會在呼叫 API 時,使用私密金鑰產生憑證。

建立服務帳戶並下載私密金鑰檔案

Create a service account:

  1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
  2. In the Google Cloud console, go to the Create service account page.

    Go to Create service account
  3. Select your project.
  4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

    In the Service account description field, enter a description. For example, Service account for quickstart.

  5. Click Create and continue.
  6. Grant the Project > Owner role to the service account.

    To grant the role, find the Select a role list, then select Project > Owner.

  7. Click Continue.
  8. Click Done to finish creating the service account.

    Do not close your browser window. You will use it in the next step.

Create a service account key:

  1. In the Google Cloud console, click the email address for the service account that you created.
  2. Click Keys.
  3. Click Add key, and then click Create new key.
  4. Click Create. A JSON key file is downloaded to your computer.
  5. Click Close.

在您的環境中使用服務帳戶金鑰檔案

Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable applies only to your current shell session. If you want the variable to apply to future shell sessions, set the variable in your shell startup file, for example in the ~/.bashrc or ~/.profile file.

Linux 或 macOS

export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

Replace KEY_PATH with the path of the JSON file that contains your credentials.

For example:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

Windows

For PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

Replace KEY_PATH with the path of the JSON file that contains your credentials.

For example:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

For command prompt:

set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH

Replace KEY_PATH with the path of the JSON file that contains your credentials.