Microsoft Azure 云服务管理 API 失败并显示 401:未授权错误?

时间:2021-04-12 12:13:38

标签: azure-cloud-services azure-management-api

我们正在集成来自 Microsoft Azure 云管理 API 的 Role Assignments - List API,文档链接:https://docs.microsoft.com/en-us/rest/api/authorization/roleassignments/list#errordetail

我们已经完成了所有提到的配置:

  • 使用 App Registrations 选项向 Azure Active Directory 注册了用于 OAuth 的多租户 Web 应用,
  • 还在 Azure 服务管理下启用了 https://management.azure.com/user_impersonation 范围
  • 网络应用请求相同的范围

到目前为止,OAuth 成功,但在用于调用 API GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleAssignments?api-version=2015-07-01 时收到的访问令牌失败并显示 401 Unauthorized 错误。在进行实际调用时,我已将 subscriptionId 替换为适当的值。

我使用 https://jwt.io/ 查看了访问令牌的详细信息,scp 元素似乎只有 "scp": "User.Read" 范围,缺少 user_impersonation。尽管来自 Microsoft 登录服务的 AUTH 对话框清楚地显示了请求的 user_impersonation 授权。我用于 OAuth 的用户帐户可以访问给定的 azure 订阅。

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

在请求访问令牌时添加 scopehttps://management.azure.com/user_impersonation 很重要。

在浏览器中使用 implicit grant flow 进行测试:

https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize?
client_id=<your-app-id>
&response_type=token
&redirect_uri=<your-redirect_uri>
&scope=https://management.azure.com/user_impersonation
&response_mode=fragment
&state=12345
&nonce=678910

enter image description here

注意:如果您使用 client credentials flow,请将 scope 更改为 https://management.azure.com/.default