I'm trying to get Azure Billing data of my subscription data by using Powershell.
mainly checked the Doc from MSDN https://learn.microsoft.com/ja-jp/rest/api/consumption/usagedetails/list
and a sample as below. 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"
After I got the authentication token successfully, got error when running request uri like
“AuthenticationFailed”, the client 'XXXXXX' with object id 'XXXX' does not have authorization to perform action 'Microsoft.Consumption/usageDetial/read' over scope '/subscriptions/XXXX' or the scope is invalid. If access was recently granted, please refresh your credential.
Might because I didn't use APPID and genarated APPkey to get credentials, instead using client_secret of application as I get token in Graph API?