【问题标题】:PermissionDenied: 403 error when I'm using Google Cloud Billing Budget APIPermissionDenied:使用 Google Cloud Billing Budget API 时出现 403 错误
【发布时间】:2021-07-26 03:19:24
【问题描述】:

我正在使用 Google 云功能和 Cloud Billing Budget API 来获取包含我所有预算的列表,但出现以下错误:

Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 67, in error_remapped_callable return callable_(*args, **kwargs) File "/env/local/lib/python3.7/site-packages/grpc/_channel.py", line 946, in __call__ return _end_unary_response_blocking(state, call, False, None) File "/env/local/lib/python3.7/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.PERMISSION_DENIED details = "The caller does not have permission"

debug_error_string = "{"created":"@9627456.9324530376","description":"Error received from peer ipv4:54.128.19.5:443","file":"src/core/lib/surface/call.cc","file_line":1069,"grpc_message":"The caller does not have permission","grpc_status":7}" > The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 449, in run_background_function _function_handler.invoke_user_function(event_object) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 268, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 265, in call_user_function event_context.Context(**request_or_event.context)) File "/user_code/main.py", line 22, in getting_data all_budgets = client.list_budgets(request = {'parent': BILLING_ACCOUNT}) File "/env/local/lib/python3.7/site-packages/google/cloud/billing/budgets_v1/services/budget_service/client.py", line 693, in list_budgets response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) File "/env/local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__ return wrapped_func(*args, **kwargs) File "/env/local/lib/python3.7/site-packages/google/api_core/retry.py", line 290, in retry_wrapped_func on_error=on_error, File "/env/local/lib/python3.7/site-packages/google/api_core/retry.py", line 188, in retry_target return target() File "/env/local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 69, in error_remapped_callable six.raise_from(exceptions.from_grpc_error(exc), exc) File "<string>", line 3, in raise_from google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission

我所做的是在组织级别为该功能的服务帐户授予适当的权限(billing.budget.list ... billing.budget.get ... etc),但它不起作用。

我的代码是这样的:

#main.py

import os
import get_budgets
from google.cloud.billing import budgets

def getting_data(data, context):
    BILLING_ACCOUNT = 'billingAccounts/XXXXXX-XXXXXX-XXXXXX'
    client = budgets.BudgetServiceClient()
    
    all_budgets = client.list_budgets(request = {'parent': BILLING_ACCOUNT})
    budget_actions.budget_list(all_budgets)
#get_budgets.py

from google.cloud.billing import budgets
from googleapiclient import discovery

#BUDGET LIST
def budget_list(all_budgets):
    print('Budget summary')
    
    for budget in all_budgets:
        print(f'Name: {budget.display_name}')

        b_amount = budget.amount
        if 'specified_amount' in b_amount:
            print(f'Specified Amount: {b_amount.specified_amount.units} {b_amount.specified_amount.currency_code}')

        if 'last_period_amount' in b_amount:
            print('Dynamic spend (based on last period)')

        print('')

有什么我忘记了吗?

【问题讨论】:

  • 显示产生错误的代码。包括创建客户端的代码,以显示您如何授权客户端。
  • 我刚刚编辑了问题,并添加了我的源代码。谢谢:)
  • 服务帐号是结算帐号的成员吗?您的问题没有显示您如何授权客户。您正在使用 ADC,但您如何知道正在使用哪些凭据?注意:拥有正确的角色是不够的。服务帐号还必须是对结算帐号具有权限的成员。
  • 我在组织级别创建了一个自定义角色,其中包含必要的权限。然后,我将自定义角色分配给我的函数的服务帐户。不,服务帐户不是计费帐户的成员。可能是错误?还是有别的?
  • 是的,服务帐号必须作为会员添加到结算帐号中。重读我的上一条评论。

标签: python google-cloud-platform google-cloud-functions


【解决方案1】:

我的问题的解决方案比我预期的要容易。

我所做的是将服务帐户添加为计费帐户的成员。

这是来自 Google 的快速视频,介绍了如何做到这一点

https://www.youtube.com/watch?v=Vti0OGQfLHQ

在我的问题的 cmets 中,您可以找到更多详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多