為開發環境設定 Service Usage
Service Usage API 可讓您管理Google Cloud 專案中的 API 和服務。在本快速入門導覽課程中,您將設定開發環境,與 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, if it is not already enabled.
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, if it is not already enabled.
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.- 確認您的使用者帳戶已獲派服務使用情形管理員角色。
使用 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 和服務,請參閱「列出服務」。