无法在Python API中为新的Google Cloud Platform项目计费

时间:2018-12-18 22:19:29

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

该项目是由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,但没有任何效果。任何可能的解决方案?

3 个答案:

答案 0 :(得分:2)

请使用下面的代码,这些 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())

答案 1 :(得分:1)

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

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

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

enter image description here

现在,我们进入错误消息的第二部分。您实际上需要等待大约5分钟。以下错误消息实际上要求您执行此操作。

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

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

答案 2 :(得分:0)

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

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