我想通过用 Python 编写的 Cloud Functions来利用 beta BillingBudgets API。使用Billing API,使用 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,但我正在努力制定如何在 Cloud Function 中实现这一点。目前,我的 requirements.txt 中有以下内容,我正在尝试研究如何通过 OAuth 来利用 Rest API。
google-api-python-client==1.7.4
oauth2client==4.1.3
google-auth-httplib2==0.0.3
欢迎任何代码片段、想法或建议。
我自己试过了,好像错误就在这一行:
budget_info = service.budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longJSONStringHere}).execute()
您缺少billingAccounts
此服务路径上的资源,您可以通过像这样添加它来修复它,它应该可以工作:
预算信息 = 服务。billingAccounts() .budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longJSONStringHere}).execute()
我注意到的另一件事是,您正在使用该oauth2client
库来检索应用程序默认凭据,但是该库已被弃用。
相反,您应该google-auth
为此使用该库,您只需更改代码即可检索凭据,如下所示:
import google.auth
credentials, project_id = google.auth.default()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句