在Google云平台上使用云功能创建预算警报时,权限被拒绝

问题描述

我想通过使用Beta BillingBudgets API用Python编写的Cloud Function创建预算警报。

我的云函数是用Python编写的,位于一个管理项目中,旨在为同一父计费帐户中的每个现有项目创建一个预算警报。

我很容易获得项目清单和现有预算,但是现在我无法成功创建预算。 我总是得到以下回应。

{
  "error": {
    "code": 403,"message": "The caller does not have permission","status": "PERMISSION_DENIED"
  }
}

很明显的消息,但是云功能使用的服务帐户具有“计费帐户管理员”角色,因此我无法确定缺少哪个权限。

功能代码提取

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,{
                "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调用

此外,即使使用我的个人帐户(与API资源管理器一起使用),也可以通过curl命令提交请求,从而导致同样的未授权错误

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,{"thresholdPercent":1,"spendBasis":"CURRENT_SPEND"}],"allUpdatesRule":{"pubsubTopic":"projects/project-billing/topics/budgets-alerts","schemaVersion":"1.0"}}}' \
  --compressed

所以我怀疑是OAuth问题,但是在围绕OAuth凭据和令牌生成进行多种变通之后,我仍然不知道。

欢迎任何想法或建议。 谢谢

解决方法

我通过将GCP项目的所有者角色分配给云功能使用的服务帐户来解决此问题。 但是我仍然不清楚为什么需要此权限(也许是为了API访问权限?)

,

在 Google 云平台中使用云功能创建预算提醒时权限被拒绝:

解决方案:

如果您计划为程序化预算通知配置 Pub/Sub 主题,您还需要将服务帐户添加为您的 Pub/Sub 主题所在项目的成员,并将 IAM 安全管理员角色分配给该服务帐户。

为了清楚起见,请参阅链接

https://cloud.google.com/billing/docs/how-to/budget-api-setup