简体   繁体   English

Python 来自 CloudRun 容器实例的 gCloud 计费 API 出现 404 错误

[英]Python gCloud billing APIs from CloudRun container instance gives 404 error

The account that is running cloudrun has been linked to the billing account, but still the cloud billing python apis throw errors.运行cloudrun的账号已经绑定了billing账号,但是cloud billing python api还是报错。 What else needs to be done in the service account?在服务帐户中还需要做什么?

#1. #1。 Created json token for service account and passed into the access_bills() method为服务帐户创建了 json 令牌并传递给 access_bills() 方法

#2. #2。 The service account has role for Billing Access View该服务帐户具有账单访问视图的角色

#3. #3。 Copied these methods as advised in comments from John Hanley's blog:按照 John Hanley 博客评论中的建议复制了这些方法:

def load_private_key(json_cred):
    ''' Return the private key from the json credentials '''

    return json_cred['private_key']


def create_signed_jwt(pkey, pkey_id, email, scope):
    '''
    Create a Signed JWT from a service account Json credentials file
    This Signed JWT will later be exchanged for an Access Token
    '''

    # Google Endpoint for creating OAuth 2.0 Access Tokens from Signed-JWT
    auth_url = "https://www.googleapis.com/oauth2/v4/token"
    expires_in = 3600

    issued = int(time.time())
    expires = issued + expires_in   # expires_in is in seconds

    # Note: this token expires and cannot be refreshed. The token must be recreated

    # JWT Headers
    additional_headers = {
            'kid': pkey_id,
            "alg": "RS256", # Google uses SHA256withRSA
            "typ": "JWT"
    }

    # JWT Payload
    payload = {
        "iss": email,       # Issuer claim
        "sub": email,       # Issuer claim
        "aud": auth_url,    # Audience claim
        "iat": issued,      # Issued At claim
        "exp": expires,     # Expire time
        "scope": scope      # Permissions
    }

    # Encode the headers and payload and sign creating a Signed JWT (JWS)
    sig = jwt.encode(payload, pkey, algorithm="RS256", headers=additional_headers)

    return sig


def exchangeJwtForAccessToken(signed_jwt):
    '''
    This function takes a Signed JWT and exchanges it for a Google OAuth Access Token
    '''

    auth_url = "https://www.googleapis.com/oauth2/v4/token"

    params = {
        "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
        "assertion": signed_jwt
    }

    r = requests.post(auth_url, data=params)

    if r.ok:
        return(r.json()['access_token'], '')

    return None, r.text


def access_bills(sa_json):
    cred = json.loads(sa_json)
    private_key = load_private_key(cred)
    # scopes = "https://www.googleapis.com/auth/cloud-platform" # this does not work, gets 404
    scopes = "https://www.googleapis.com/auth/cloud-billing.readonly"

    s_jwt = create_signed_jwt(
            private_key,
            cred['private_key_id'],
            cred['client_email'],
            scopes)

    token, err = exchangeJwtForAccessToken(s_jwt)

    if token is None:
        logger.error("Error: {}".format(err))
        exit(1)

    
    logger.info("Token response: {}".format(token))

    # the token is obtained and prints in the log

    headers = {
        "Host": "www.googleapis.com",
        "Authorization": "Bearer " + token,
        "Content-Type": "application/json"
    }

    try:
        url = "https://cloudbilling.googleapis.com/v1/billingAccounts/01C8DC-336472-E177E1" # account name is "Billing Core"
        response = requests.get(url=url, headers=headers)
        logger.info("Response: {}".format(response))

        # logs -> app - INFO - Response: <Response [404]>

        return {
            'statusCode': 200,
            'body': 'Success'
        }
    except Exception as e:
        logger.error("Error")
        raise e

It gives 404 error as shown in the comment log after trying on that url.在尝试 url 后,它给出了 404 错误,如评论日志中所示。

Okay I found that the only way it works as of now is via big query export from billing account, sort out dataViewer permission and run the corresponding sql from python application, it works.好的,我发现它目前唯一有效的方法是通过从计费帐户导出大查询,整理 dataViewer 权限并从 python 应用程序运行相应的 sql,它有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 CloudRun 错误“请求已中止,因为没有可用实例” - How to fix CloudRun error 'The request was aborted because there was no available instance' ERROR: (gcloud.sql.instances.describe) HTTPError 404: Cloud SQL 实例不存在,但确实存在? - ERROR: (gcloud.sql.instances.describe) HTTPError 404: The Cloud SQL instance does not exist, but it does exist? 从Azure数据工厂调用Azure Function报404错误 - Calling Azure Function from Azure Data Factory Gives 404 Error 从 C# CloudRun 连接 CloudSQL MySQL public IP 时出错 - Error when connecting CloudSQL MySQL public IP from C# CloudRun Gcloud CLI 使用哪些 API - Gcloud CLI using what APIs 谷歌云 SDK:gcloud auth login 给我一个错误 - Google Cloud SDK: gcloud auth login gives me an error 通过 gcloud 或 python 获取“logging.googleapis.com/billing/monthly_bytes_ingested”项目信息 - get "logging.googleapis.com/billing/monthly_bytes_ingested" project info by gcloud or python 错误:gcloud 加载失败:没有名为“googlecloudsdk.third_party.apis.binaryauthorization.v1alpha2.binaryauthorization_v1alpha2_messages”的模块 - ERROR: gcloud failed to load: No module named 'googlecloudsdk.third_party.apis.binaryauthorization.v1alpha2.binaryauthorization_v1alpha2_messages' 不断获取权限错误 gcloud.container.clusters.get-credentials - Keep getting permissions error gcloud.container.clusters.get-credentials Gcloud/GCP:项目存在但我收到错误 404 资源项目/<project_id> 没有找到</project_id> - Gcloud/GCP : Project exists but I get an Error 404 The resource project/<project_id> was not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM