发送请求

本主题介绍了如何向 Cloud Billing API 发送一些简单的请求。

如需查看完整的方法列表,请参阅 RESTRPC 参考文档。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. Enable the Cloud Billing API.

    Enable the API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. 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.

  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  9. 确保您的 Google Cloud 项目已启用结算功能

  10. Enable the Cloud Billing API.

    Enable the API

  11. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  12. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  13. 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 账号(RESTRPC)的 API 方法是 API 中最简单的方法,因为该方法无需设置参数,所以很适合用来入门。该方法会直接返回您有权查看的所有结算账号。

协议

GET https://cloudbilling.googleapis.com/v1/billingAccounts

Java

    ListBillingAccountsResponse result = service.billingAccounts().list().execute();

检索特定的 Cloud Billing 账号

若要检索特定的结算账号(RESTRPC),您需要知道结算账号 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 账号相关联(RESTRPC)。下面的示例代码会将 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();

清理

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.