本指南介绍了开始使用 Enterprise Knowledge Graph 所需的所有设置步骤。
Google Cloud 控制台简介
Google Cloud console 是一个网页界面,用于预配、配置、管理和监控使用 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
启用 API
您必须为项目启用 Enterprise Knowledge Graph API。 如需详细了解如何启用 API,请参阅 Service Usage 文档。
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.
设置身份验证
任何使用 API 的客户端应用都必须经过身份验证,并且拥有所请求资源的访问权限。本部分介绍重要的身份验证概念及其设置步骤。如需了解详情,请参阅身份验证概览。
服务账号简介
身份验证有多种可选方式,但建议您使用服务账号执行身份验证和访问权限控制。服务账号为应用而非最终用户提供凭据。项目拥有自己的服务账号。 您可以为一个项目创建多个服务账号。 如需了解详情,请参阅服务账号。
角色简介
在调用 API 时, Google Cloud 要求调用方身份(任何适用的用户、实体或进程及其指定的属性)拥有适当的权限。 您可以通过向服务账号授予角色来授予权限。如需了解详情,请参阅 Identity and Access Management (IAM) 文档。
如需试用 Enterprise Knowledge Graph API,您可以在以下步骤中使用项目 > Owner 角色。 项目 > Owner 角色会为服务账号授予对项目的完整访问权限。 如果您的服务账号不需要完整权限,请使用Google Cloud 控制台指定更具限制性的角色。 如需查看 Enterprise Knowledge Graph 的权限和角色列表,请参阅 Enterprise Knowledge Graph 权限和 Enterprise Knowledge Graph 角色。 如需了解如何使用 IAM 角色管理权限,请参阅向服务账号授予角色。
服务账号密钥简介
服务账号与一个或多个公钥/私钥对相关联。 创建新密钥对时,您需要下载私钥。Google Cloud CLI 会在调用 API 时使用您的私钥来生成凭据。
创建服务账号并下载私钥文件
Create a service account:
-
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. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
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:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- 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_PATHReplace KEY_PATH with the path of the JSON file that contains your credentials.