【问题标题】:Can't enable billing for a fresh Google Cloud Platform project in Python API无法在 Python API 中为新的 Google Cloud Platform 项目启用计费
【发布时间】:2018-12-18 22:19:29
【问题描述】:

该项目是由 Python 使用 google_application_defaults 创建的,计费 API 已成功激活并在命令行上使用

os.system(gcloud services enable cloudbilling.googleapis.com)
os.system(gcloud services list --enabled)

然后,运行时:

from googleapiclient import discovery, errors, logging
CB = discovery.build("cloudbilling", "v1", credentials = default_creds,  cache_discovery = False)   
billing_body = {"projectID": projectID, "name": "projects/"+projectID+"/billingInfo", "billingEnabled": True, "billingAccountName": "billingAccounts/"+billingAccount}
billingUP = CB.projects().updateBillingInfo(name = projectID, body = billing_body )
try:
    billing_resp = billingUP.execute()
    print("Billing succesfully enabled")
except errors.HttpError as err:
    billing_resp = None
    print("request voided")
    logging.error("There was an error creating the project. Check:")
    logging.error(err._get_reason())

我收到以下错误:

Cloud Billing API has not been used in project XXXXXXXXXX before or it  is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudbilling.googleapis.com/overview?project=XXXXXXXXXXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

我也尝试在 python 脚本之外的命令行上直接激活它,但错误仍然存​​在。我已经在 billingUp 请求的“projectID”部分使用了不同的格式:projectName、projects/projectName、projectID、projects/projectID,但没有任何效果。任何可能的解决方案?

【问题讨论】:

  • 是否确实启用了计费,但尚未传播?
  • 试试这个命令:gcloud beta billing accounts list 如果这报告了一个 API 错误,你还没有启用计费。如果它有效,那么您有代码问题,或者您没有等待足够长的时间来启用 API。
  • @JohnHanley 该命令正常工作,显示帐户名称、ID 和状态。
  • @DustinIngram Uhm,我对此表示怀疑,因为我已经尝试了两天。
  • 验证项目帐号是否与您正在使用的服务帐户凭据的项目匹配。

标签: python google-cloud-platform gcloud google-api-client


【解决方案1】:

请使用下面的代码,这些 GCP 文档非常不清楚,难以理解和实现

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
projectID="<project id>"
biling_account_id = "<billing account id>"
credentials = GoogleCredentials.get_application_default()
from googleapiclient import discovery, errors, logging
CB = discovery.build("cloudbilling", "v1", credentials = credentials)   
 
billing_body = {
  "name": "projects/"+ projectID+"/billingInfo",
  "projectId": projectID,
  "billingEnabled": True,
  "billingAccountName": "billingAccounts/"+biling_account_id
}


print(billing_body)

billingUP = CB.projects().updateBillingInfo(name = "projects/"+ projectID,body=billing_body)
try:
    billing_resp = billingUP.execute()
    print("Billing succesfully enabled")
except errors.HttpError as err:
    billing_resp = None
    print("request voided")
    logging.error("There was an error enabling billing for the project. Check:")
    logging.error(err._get_reason())

【讨论】:

    【解决方案2】:

    响应中给出的错误信息足够丰富。在 GCP 中使用特定 API 或服务之前,您需要先使用控制台启用它们。在此示例中,Cloud Billing API 未启用。转到以下网址。 (此 URL 也在响应中给出)。

    https://console.developers.google.com/apis/api/cloudbilling.googleapis.com/overview?project=XXXXXXXXXXX
    

    XXXXXXXXXXX 替换为项目 ID。您将看到如下所示的界面。点击ENABLE

    现在我们来到错误消息的第二部分。你实际上需要等待5分钟。以下错误消息实际上要求您这样做。

    If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
    

    现在运行您的 API 查询,您将得到响应。

    【讨论】:

      【解决方案3】:

      我只是做了一个变通方法,非常原始的解决方案,但有效。希望有另一种方法来做到这一点。我所做的只是使用 os.system 调用命令 shell 并使用 gcloud 来启用它:

       billing_command = "gcloud beta billing projects link {} --billing-account {}".format(projectID, billingAccount)
      os.system(billing_command)
      

      【讨论】:

        猜你喜欢
        • 2021-02-04
        • 1970-01-01
        • 2021-03-25
        • 2019-03-28
        • 1970-01-01
        • 2021-12-09
        • 2019-11-09
        • 2016-04-08
        • 2020-02-10
        相关资源
        最近更新 更多