本页描述如何设置开发环境来使用 Service Management API。使用 Service Management API 的最简单方法(不仅仅是使用 Google Cloud 控制台网络用户界面),对于大多数操作用例,我们的建议是通过 gcloud
命令行接口。如果您需要针对 Service Management API 进行编程,则可以使用我们提供的某一客户端库。若要试用该 API,您可以按照下面的备用设置说明进行操作,并使用 curl 命令来使用该 API,而无需设置完整的应用开发环境。
使用 gcloud 进行设置
-
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 Management API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
配置 gcloud CLI 以使用您的联合身份。
如需了解详情,请参阅使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init设置为直接调用 API
本节描述了通过使用
curl命令的 Service Management API 设置本地环境以进行试验所必需的基本步骤。这种设置适用于需要针对 Service Management API 进行编程的开发者。初始设置
-
Install the Google Cloud CLI.
-
配置 gcloud CLI 以使用您的联合身份。
如需了解详情,请参阅使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
Create or select 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Enable the Service Management API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable servicemanagement.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/servicemanagement.admingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For examples, see Represent workforce pool users in IAM policies.ROLE: The IAM role that you grant to your user account.
为调用 Google REST API 定义一个方便的 shell 别名:
$ alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json"'尝试列出可公开访问的服务:
$ gcurl https://servicemanagement.googleapis.com/v1/services如果您看到服务列表,则说明设置成功。
用 curl 进行测试
后续步骤
如果您要创建托管式服务但没有使用 Cloud Endpoints,请按照下列步骤操作:
-