我想通过使用 beta BillingBudgets API 用 Python 编写的云函数创建预算警报。
我的云函数是用 Python 编写的,位于一个管理项目中,旨在为同一父计费帐户中的每个现有项目创建一个预算警报。
我很容易得到项目清单和现有预算,但现在我无法成功创建预算。我总是得到以下回应。
{
"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED"
}
}
很明显的消息,但是云功能使用的服务帐户具有 Billing account Administrator 角色,所以我无法弄清楚缺少哪个权限。
云功能代码提取:
import google.auth
from googleapiclient import discovery
scopes = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-billing']
credentials, project_id = google.auth.default(scopes)
billingbudgetsService = discovery.build('billingbudgets', 'v1beta1', credentials=credentials, cache_discovery=False)
billing_account = "billingAccounts/000000-AAAAAA-BBBBBB"
budget_body = {
"budget": {
"displayName": "MyBudget",
"budgetFilter": {
"projects": [
"projects/11111111"
],
"creditTypesTreatment": "INCLUDE_ALL_CREDITS"
},
"amount": {
"specifiedAmount": {
"currencyCode": "EUR",
"units": "300"
}
},
"thresholdRules": [
{
"thresholdPercent": 0.5,
"spendBasis": "CURRENT_SPEND"
},
{
"thresholdPercent": 0.9,
"spendBasis": "CURRENT_SPEND"
},
{
"thresholdPercent": 1,
"spendBasis": "CURRENT_SPEND"
}
],
"allUpdatesRule": {
"pubsubTopic": "projects/billing-project/topics/budgets-alerts",
"schemaVersion": "1.0"
}
}
}
res = billingbudgetsService.billingAccounts().budgets().create(parent=billing_account, body=budget_body).execute()
我已经使用我的个人帐户(也是计费帐户管理员)通过API Explorer验证了 API 调用。
此外,我通过 curl 命令提交请求时遇到了同样的未经授权的错误,即使使用我的个人帐户(与 API Explorer 一起使用)也是如此
curl --request POST \
'https://billingbudgets.googleapis.com/v1beta1/billingAccounts/AAAAAA-BBBBBB-DDDDDDD/budgets' \
--header "authorization: Bearer $(gcloud auth application-default print-access-token)" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"budget":{"displayName":"mybudget","budgetFilter":{"projects":["projects/11111111111"],"creditTypesTreatment":"INCLUDE_ALL_CREDITS"},"amount":{"specifiedAmount":{"currencyCode":"EUR","units":"300"}},"thresholdRules":[{"thresholdPercent":0.5,"spendBasis":"CURRENT_SPEND"},{"thresholdPercent":0.9,"spendBasis":"CURRENT_SPEND"},{"thresholdPercent":1,"spendBasis":"CURRENT_SPEND"}],"allUpdatesRule":{"pubsubTopic":"projects/project-billing/topics/budgets-alerts","schemaVersion":"1.0"}}}' \
--compressed
所以我怀疑是 OAuth 问题,但是在围绕 OAuth 凭据和令牌生成进行了多次解决之后,我仍然不知道。
欢迎任何想法或建议。谢谢