
Estou criando um script do PowerShell para obter métricas do Azure. Tenho um registro de aplicativo com permissão de API para Insights-UserMetric.Read.All (tipo de aplicativo). Também monitora a função de Leitor atribuída a este aplicativo no nível da assinatura. Ao executar um script, estou recebendo o erro:
{ "error": { "code": "AuthenticationFailed", "message": "Authentication failed." } }
Tentei aumentar a função para Colaborador apenas para verificar se não tenho permissões suficientes, mas mesmo com a função Colaborador estou recebendo o mesmo erro
Aqui está o roteiro:
$ClientId = 'someClientId'
$ClientSecret = 'someClientSecret'
$Tenantid = 'someTenantId'
$TokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $ClientId
Client_Secret = $ClientSecret
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$Tenantid/oauth2/v2.0/token" -Method POST -Body $TokenBody
$Headers = @{
"Authorization" = "Bearer $($tokenResponse.access_token)"
"Content-type" = "application/json"
}
$Uri = 'https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.EventHub/namespaces/$eventHub/providers/Microsoft.Insights/metrics?api-version=2018-01-01'
Invoke-RestMethod -Headers $Headers -Uri $Uri -Method GET
Agradeço qualquer tipo de orientação
Responder1
Problema resolvido. Parte do token corrigida:
$TokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://management.azure.com/.default"
Client_Id = $ClientId
Client_Secret = $ClientSecret
Tenant_Id = $TenantId
}