【问题标题】:azure billing REST API天蓝色计费 REST API
【发布时间】:2020-08-11 04:37:40
【问题描述】:

我正在尝试使用 Powershell 获取订阅数据的 Azure 计费数据。

主要查看MSDN的Doc https://docs.microsoft.com/ja-jp/rest/api/consumption/usagedetails/list

和一个示例如下。 https://www.cloudnative.at/2017/12/22/generate-an-azure-consumption-report-with-the-consumption-rest-api-and-powershell/

$loginUri = "https://login.microsoft.com"
$body =@{
    client_id = XXXX
    client_secrect = XXXXXXXX
    resource    =  "https://management.core.windows.net"
    grant_type = "client_credentials"
}

$oauth = Invoke-RestMethod -Method Post -Uri $loginUrl/$TenantID/oauth2/token?api-version=1.0 -Body $body 

# SubscriptionID and Billing Period
$SubscriptionId = '<Your subscription GUID here>'
$billingperiod = '202006-1'

#Create the REST-URL
$usageURL =     "https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/billingPeriods/$billingperiod/providers/Microsoft.Consumption/usageDetails?api-version=2017-11-30"

我成功获得认证令牌后,运行请求uri时出错

“AuthenticationFailed”,对象 id 为“XXXX”的客户端“XXXXXX”无权对范围“/subscriptions/XXXX”执行操作“Microsoft.Consumption/usageDetial/read”或范围无效。如果最近授予访问权限,请刷新您的凭据。

可能是因为我没有使用 APPID 并生成 APPkey 来获取凭据,而是使用应用程序的 client_secret 作为我在 Graph API 中获取令牌的原因?

【问题讨论】:

    标签: azure-billing-api


    【解决方案1】:

    如果您想通过 Azure AD 应用程序访问 Azure 计费 api,我们需要为 AD 应用程序分配 Azure RABC 角色(Billing Reader、Reader、Owner 或 Contributor 角色)。更多详细信息,请参阅@987654321 @

    例如(我分配贡献者角色)

    第 1 步:登录您的 azure 门户
    第二步:在左侧菜单栏中找到订阅并点击。

    第 3 步:点击访问控制 IAM,然后点击添加。

    第 4 步:在“添加权限”窗口中,选择角色的贡献者。在选择输入框中,键入您在 Azure AD 中创建的应用名称(在 Azure Active Directory 中创建)并选择它。就我而言,我创建了 Azure 资源管理。

    步骤 5:在您成功授予权限后,单击订阅窗口中的刷新,您将看到您的应用程序显示在列表中。请参见下面的示例。

    第六步:Powershell 脚本

    $tenantId="76a1f773...b-86b9-d1ced3e15cda"
    $clientId="0159ec7d-f...-a680-c4d40ab7a36c"
    $clientSecret="o4eq4jj...I26uz26W~"
    $secSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
    
    $pscredential = New-Object System.Management.Automation.PSCredential ($clientId, $secSecret)
    Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId
    
    $dexResourceUrl="https://management.azure.com/"
    $context = Get-AzContext
    $token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $dexResourceUrl).AccessToken
    
    
    $SubscriptionId = '3465e081-85b6-4b54-a3e1-15675acb615f'
    $billingperiod = '202010-1'
    
    #Create the REST-URL
    $usageURL ="https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/billingPeriods/$billingperiod/providers/Microsoft.Consumption/usageDetails?api-version=2017-11-30"
    
    $header = @{
        'Authorization' = "Bearer $($token)"
        "Content-Type" = "application/json"
    }
     
    $UsageData = Invoke-RestMethod `
        -Method Get `
        -Uri $usageURL `
        -ContentType application/json `
        -Headers $header 
    
    ConvertTo-Json $UsageData
    

    【讨论】:

    • 如果它对你有用,你能接受它作为答案吗?它可能会帮助更多有类似问题的人
    • 谢谢吉姆!添加权限后它正在工作。我需要获取并与 RateCard API 结合以获取整个计费报告,如果您也有这方面的示例,您能否介绍一下?
    【解决方案2】:

    按照此处的说明,我在发出实际 API 请求时收到以下错误:

    Invoke-RestMethod : {"error":{"code":"500","message":"An error occurred during processing this request. Use this request id
        '17f6fdea-xxxx-xxxx-xxxx-a8c314d770ee' for follow-up."}}
        At C:\$Downloads\billingapitest.ps1:45 char:14
        + $UsageData = Invoke-RestMethod `
        +              ~~~~~~~~~~~~~~~~~~~
            + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
            + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    

    任何想法如何解决?

    谢谢

    达伦

    =============================================

    我向 MS 支持提交了一张票。就在安排演示该问题的会议之前,我再次测试了 Powershell 脚本,发现它现在可以工作了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多