本主题介绍了如何向 Cloud Billing API 发送一些简单的请求。
如需查看完整的方法列表,请参阅 REST 或 RPC 参考文档。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Cloud Billing API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Cloud Billing API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.
获取 Cloud Billing 账号列表
列出所有 Cloud Billing 账号(REST、RPC)的 API 方法是 API 中最简单的方法,因为该方法无需设置参数,所以很适合用来入门。该方法会直接返回您有权查看的所有结算账号。
协议
GET https://cloudbilling.googleapis.com/v1/billingAccounts
Java
ListBillingAccountsResponse result = service.billingAccounts().list().execute();
检索特定的 Cloud Billing 账号
若要检索特定的结算账号(REST、RPC),您需要知道结算账号 ID。本示例使用示例 ID 012345-567890-ABCDEF
。
协议
GET https://cloudbilling.googleapis.com/v1/billingAccounts/012345-567890-ABCDEF
Java
BillingAccount result = service.billingAccounts() .get("billingAccounts/00C5EA-61187E-D842F2").execute();
在 Google Cloud 项目中启用 Cloud Billing
若要对 Google Cloud 项目启用 Cloud Billing 功能,您需要调用 Cloud Billing API 以将该 Google Cloud 项目与现有的 Cloud Billing 账号相关联(REST、RPC)。下面的示例代码会将 Google Cloud 项目 tokyo-rain-123
与 Cloud Billing 账号 012345-567890-ABCDEF
相关联。顺便提一下,若要对 Google Cloud 项目停用 Cloud Billing 功能,也可采用相同的 API 方法,只需将 billingAccountName
设置为空即可。
协议
PUT https://cloudbilling.googleapis.com/v1/projects/tokyo-rain-123/billingInfo { "billingAccountName": "billingAccounts/012345-567890-ABCDEF" }
Java
service.projects().updateBillingInfo( "projects/tokyo-rain-123", new ProjectBillingInfo().setBillingAccountName("billingAccounts/012345-567890-ABCDEF")) .execute();
清理
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.