为开发环境设置 Service Usage

本页介绍如何设置开发环境以使用 Service Usage API。

  • 对于大多数操作用例,启用和停用服务的最简单方法是使用 Cloud de Confiance 控制台。
  • 如果需要创建脚本,可以使用 Google Cloud CLI。
  • 如果您需要针对 Service Usage API 进行编程,请使用某个客户端库
  • 如需试用 API,您可以按照本指南中的备用设置说明进行操作,并使用 curl 命令来测试 API,而无需设置完整的应用开发环境。

设置环境以直接调用 API

本节描述了通过使用 curl 命令的 Service Usage API 设置本地环境以进行试验所必需的基本步骤。本使用入门适用于需要针对 Service Usage API 进行编程的开发者。

启用 Service Usage API

如需使用 Service Usage API,您必须先在Cloud de Confiance 项目中启用该 API。

  1. In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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

  2. Enable the Service Usage API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  3. 确保您的用户账号已被授予 Service Usage Admin 角色。

用 curl 进行测试

  1. 使用项目的标识符设置环境变量 PROJECT_ID

    PROJECT_ID=PROJECT_ID
    

    PROJECT_ID 替换为您的 Cloud de Confiance 项目 ID 或编号。

  2. 确保您已登录“gcloud”:

    gcloud auth login
    
  3. 列出此项目中已启用的 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"
        }
      },
      ...
    

后续步骤

如需列出Cloud de Confiance 项目中已启用或可用的 API 和服务,请参阅列出服务