获取Azure订阅成本时遇权限错误,求解决方法
"message":"客户端'1234156-ab0b5797ef12'(对象ID为'123456-4839-a8ce-ab0b5797ef12')无权在范围'/providers/Microsoft.Management/managementGroups/stack-testing/providers/Microsoft.Consumption'上执行操作'Microsoft.Consumption/aggregatedcost/read',或范围无效。若最近已授予权限,请刷新凭据。"}}
配置对应权限
给对象ID为123456-4839-a8ce-ab0b5797ef12的客户端,在/providers/Microsoft.Management/managementGroups/stack-testing管理组层级分配Cost Management Reader或Reader角色——这两个角色包含Microsoft.Consumption/aggregatedcost/read所需权限,注意必须在管理组层级分配,仅订阅层级权限不生效。验证范围有效性
确认目标管理组存在:通过Azure CLI执行az account management-group show --name stack-testing,检查返回结果是否正常;同时确认该客户端主体已被允许访问此管理组。刷新凭据重试
若刚完成权限分配,可能存在缓存延迟。重新获取Bearer token(例如用Azure CLI命令az account get-access-token --resource https://management.azure.com/),替换CURL中的token后再次发起请求。检查API请求格式
确保CURL的请求URL正确,管理组维度的aggregatedcost API路径应为:https://management.azure.com/providers/Microsoft.Management/managementGroups/stack-testing/providers/Microsoft.Consumption/aggregatedcost?api-version=2021-10-01
注意不能省略api-version参数。
内容的提问来源于stack exchange,提问作者user630702

