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