【问题标题】:Microsoft Azure Cloud service management API fails with 401: Unauthorized error?Microsoft Azure 云服务管理 API 失败并出现 401:未经授权的错误?
【发布时间】:2021-04-12 12:13:38
【问题描述】:

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

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

  • 使用 App Registrations 选项向 Azure Active Directory 注册了多租户 Web 应用以进行 OAuth,
  • 还在 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 订阅。

可能是什么问题?

【问题讨论】:

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


    【解决方案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
    

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

    【讨论】:

    • 我删除了User.Read 权限,只保留了https://management.azure.com/user_impersonation 权限,然后它在没有 401 的情况下开始工作,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多