通过 Cloud Functions 创建 Google Cloud Platform 预算

酷星期五7

我想通过用 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 Cloud Billing Budget API 通过启用通知渠道创建预算

在Google Cloud Platform中创建实例模板

在创建后标记 Google Cloud Platform 部署

如何创建在 Google Cloud Platform 中创建的 MongoDB 用户?

Cloud Functions中的Google Identity Platform身份验证

Google Cloud Messaging VS Google Cloud Platform

使用Google Cloud Platform Deployment Manager模板创建存储桶

在Google Cloud Platform中创建HTTP负载均衡器

无法在Google Cloud Platform中创建Jupyter Notebook实例

在Google Cloud Platform上创建自动磁盘快照(备份)保留

如何在气流中创建自定义运算符并在通过Cloud Composer运行的气流模板中使用它们(在Google Cloud Platform中)

通过Google Cloud Deployment Manager创建的Google数据流

Google Cloud Functions - 重试

用于通过REST API在Google Cloud Platform中的Cloud Armor中更新规则的Python脚本

Google Cloud Platform:如何重命名Google Cloud Platform项目?

通过Cloud Functions触发Google Container Builder构建

通过 Google Cloud Functions 的二进制响应

尝试通过 Google Cloud Functions 托管此 Express 应用

Google Cloud Platform文件位置

Google Cloud Platform个人帐单

Google Cloud Platform数据显示

Google Cloud Platform修复SSH

如何通过Google Cloud Functions部署与PostgreSQL(梁块)连接的Google Cloud Dataflow

为Google Cloud Functions创建开发和登台环境

如何从Cloud Functions连接Google Cloud SQL?

如何在Google Cloud Platform中创建用户而无需创建新的Gmail用户?

Google Cloud Platform Postman 仅通过主 Google 帐户而不是服务帐户调用函数

通过Cloud Run运行AI Platform Notebook

Google Cloud Platform和Google Cloud Firestore之间的区别?