為開發環境設定服務使用情形

本頁面說明如何設定開發環境以使用 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. 使用專案的 ID 設定環境變數 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 和服務,請參閱「列出服務」。