代码之家  ›  专栏  ›  技术社区  ›  NoviceMe

尝试在IBM云函数中运行代码

  •  0
  • NoviceMe  · 技术社区  · 7 年前

    iam_token = 'Bearer eyJraWQiOiIyMDE3MTAzMC0wM****'
    def processResourceInstanceUsage(account_id, billMonth):
        METERING_HOST = "https://metering-reporting.ng.bluemix.net"
        USAGE_URL = "/v4/accounts/"+account_id + \
            "/resource_instances/usage/"+billMonth+"?_limit=100&_names=true"
    
        url = METERING_HOST+USAGE_URL
        headers = {
            "Authorization": "{}".format(iam_token),
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        response = requests.get(url, headers=headers)
        print("\n\nResource instance usage for first 100 items")
        return response.json()
    
    processResourceInstanceUsage('*****', '11')
    

    由于某种原因,我不断地犯错误。我多次尝试创建iam_令牌。它仍然给出相同的错误。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Vidyasagar Machupalli    7 年前

    在您提供的代码中,应该注意的事情很少。

    • YYYY-MM 总体安排

    • 帐号 应该是你旁边的id 账户 当你跑步的时候,你的名字 ibmcloud target

    • ibmcloud iam oauth_tokens . 如果要生成 访问令牌 使用您的平台API密钥,请参阅 link . 字眼 不需要,因为这不是授权令牌。

    import sys
    import requests
    
    def main(dict):
        METERING_HOST="https://metering-reporting.ng.bluemix.net"
        account_id="3d40d89730XXXXXXX"
        billMonth="2018-10"
        iam_token="<IAM_TOKEN> or <ACCESS_TOKEN>"
        USAGE_URL="/v4/accounts/"+account_id+"/resource_instances/usage/"+billMonth+"?_limit=100&_names=true"
    
        url=METERING_HOST+USAGE_URL
        headers = {
            "Authorization": "{}".format(iam_token),
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        response=requests.get(url, headers=headers)
        print ("\n\nResource instance usage for first 100 items")
        return { 'message': response.json() }
    

    这对我来说很有效,并返回了一个JSON,其中包含按区域计费的数据。

    参考: https://stackoverflow.com/a/52333233/1432067

    推荐文章