Google cloud platform 通过云功能创建谷歌云平台预算

Google cloud platform 通过云功能创建谷歌云平台预算,google-cloud-platform,google-cloud-functions,google-oauth,google-cloud-billing,Google Cloud Platform,Google Cloud Functions,Google Oauth,Google Cloud Billing,我想通过一个用Python编写的云函数来利用beta版。有了,使用API来更新项目的账单是非常简单的 from oauth2client.client import GoogleCredentials credentials = GoogleCredentials.get_application_default() from apiclient import discovery service = discovery.build('cloudbilling', 'v1', credent

我想通过一个用Python编写的云函数来利用beta版。有了,使用API来更新项目的账单是非常简单的

from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()    
from apiclient import discovery
service = discovery.build('cloudbilling', 'v1', credentials=credentials, cache_discovery=False)
billing_info = service.projects().updateBillingInfo(name='projects/{}'.format(projectID), body={'billingAccountName': 'billingAccounts/000000-AAAAAA-BBBBBB'}).execute()
但是,尝试使用BillingBudgets API创建一个预算,例如

from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
from apiclient import discovery
service = discovery.build('billingbudgets', 'v1beta1', credentials=credentials, cache_discovery=False)
budget_info = service.budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longJSONStringHere}).execute()
…因“'Resource'对象没有属性'billing'”而失败。浏览API并使用对象语法和结构并没有产生任何结果。从文档中可以看出,目前还没有适用于此API的客户端库,因此我目前假设这将在将来起作用,并且我现在正在寻找一种利用此API的替代方法

我可以直接使用REST和OAuth成功地运行API,但我正在努力制定如何在云函数中实现这一点。目前,我的requirements.txt中有以下内容,并且正在尝试通过OAuth来利用RESTAPI

google-api-python-client==1.7.4
oauth2client==4.1.3
google-auth-httplib2==0.0.3

欢迎您提供任何代码片段、想法或建议。

我自己也尝试过,错误似乎在这一行:

预算信息= service.budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longjsonstringher}).execute()

您缺少此服务路径上的资源,您可以通过如下方式添加它来修复它,它应该可以工作:

预算信息= 服务。billingAccounts().budgets()。创建('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longjsonstringher}).execute()

我注意到的另一件事是,您正在使用该库检索应用程序默认凭据,但是该库已被弃用

相反,您应该为此使用库,您只需更改代码以检索凭据,如下所示:

import google.auth

credentials, project_id = google.auth.default()

我终于明白了

# for Cloud Functions use
def get_service():
    import googleapiclient.discovery
    return googleapiclient.discovery.build('compute', 'v1',  cache_discovery=False)
requirements.txt

google-api-python-client
oauth2client