为开发环境设置 Service Usage
本页介绍如何设置开发环境以使用 Service Usage API。
- 对于大多数操作用例,启用和停用服务的最简单方法是使用 Google Cloud 控制台。
- 如果需要创建脚本,可以使用 Google Cloud CLI。
- 如果您需要针对 Service Usage API 进行编程,请使用某个客户端库。
- 如需试用 API,您可以按照本指南中的备用设置说明进行操作,并使用
curl命令来测试 API,而无需设置完整的应用开发环境。
设置环境以直接调用 API
本节描述了通过使用 curl 命令的 Service Usage API 设置本地环境以进行试验所必需的基本步骤。本使用入门适用于需要针对 Service Usage API 进行编程的开发者。
启用 Service Usage API
如需使用 Service Usage API,您必须先在Google Cloud 项目中启用该 API。
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
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.
Enable the Service Usage API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
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.
Enable the Service Usage API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- 确保您的用户账号已被授予 Service Usage Admin 角色。
用 curl 进行测试
使用项目的标识符设置环境变量
PROJECT_ID:PROJECT_ID=PROJECT_ID将 PROJECT_ID 替换为您的 Google Cloud 项目 ID 或编号。
确保您已登录“gcloud”:
gcloud auth login列出此项目中已启用的 API 和服务:
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" "https://serviceusage.googleapis.com/v1/projects/${PROJECT_ID}/services?filter=state:ENABLED&fields=services.config.title,services.config.name"如果您看到如下所示的输出,则表示设置成功:
{ "services": [ { "config": { "name": "bigquery.googleapis.com", "title": "BigQuery API" } }, { "config": { "name": "bigquerystorage.googleapis.com", "title": "BigQuery Storage API" } }, ...
后续步骤
如需列出Google Cloud 项目中已启用或可用的 API 和服务,请参阅列出服务。