微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

天青计费REST API

如何解决天青计费REST API

我正在尝试使用Powershell获取订阅数据中的Azure账单数据。

主要从MSDN检查了文档 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来获取凭据,而是在Graph API中获得令牌时使用了应用程序的client_secret吗?

解决方法

如果要使用Azure AD应用程序访问Azure计费api,我们需要为AD应用程序分配Azure RABC角色(计费读取器,读取器,所有者或贡献者角色)。有关更多详细信息,请参考{{ 3}} document

例如(我指定了贡献者角色)

第1步:登录到Azure门户
步骤2:在左侧菜单栏中找到“订阅”,然后单击。
enter image description here

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

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

第5步:获得成功的权限后,在订阅窗口中单击“刷新”,您将看到您的应用程序显示在列表中。请参见以下示例。 enter image description here

第6步: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,$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"
}
 
= Invoke-RestMethod `
    -Method Get `
    -Uri $usageURL `
    -ContentType application/json `
    -Headers $header 

ConvertTo-Json $UsageData
,

按照这里的说明进行操作时,发出实际的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

任何想法如何解决?

谢谢

Darren

========================================== >

我提交了具有MS支持的票证。就在计划演示该问题的会话之前,我又测试了Powershell脚本一次,发现它现在可以工作了!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。