azure - Microsoft Azure OAuth 客户端凭据 token 获得 "AuthorizationFailed"响应

标签 azure oauth-2.0 azure-active-directory azure-api-management azure-authentication

我想通过 REST API 创建 APIM 订阅,并且能够按照此 Microsoft 文档 https://learn.microsoft.com/en-us/rest/api/apimanagement/current-ga/subscription 成功完成此操作。 .

对于身份验证,我使用 ROPC 授予类型(我的用户名和密码)生成不记名 token 。按照此流程,一切正常。

但我不想在应用程序中配置我的用户名和密码来获取不记名 token ,而是遵循客户端凭据授予类型(通过客户端 ID 和 secret 获取 token ),我能够生成 token ,但是当我使用该 token 在 APIM 中创建订阅,我遇到异常 客户端“0--e”,对象 ID 为“0” --e' 无权执行操作“Microsoft.ApiManagement/service/subscriptions/write”

是否可以在 APIM AccessControl(IAM) 中添加 AAD 应用程序来授予权限。 或者这是任何其他方式来做到这一点?或者ROPC是唯一的方法?

有人可以帮忙吗?

最佳答案

Yes, you can grant permission to AAD application (service principal) in APIM Access Control (IAM) by assigning it API Management Service Contributor role.

我尝试在我的环境中重现相同的结果并得到以下结果:

我使用客户端凭据授予类型生成了一个访问 token ,如下所示:

enter image description here

当我使用上述 token 通过以下查询创建 APIM 订阅时,我收到了相同的错误:

PUT https://management.azure.com/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.ApiManagement/service/apimService1/subscriptions/testsub?api-version=2021-12-01

{
  "properties": {
    "ownerId": "/subscriptions/subid/resourceGroups/rgname/providers/Microsoft.ApiManagement/service/servicename/users/xxxxxxxxxxx",
    "scope": "/subscriptions/subid/resourceGroups/rgname/providers/Microsoft.ApiManagement/service/servicename/products/xxxxxxxxxxx",
    "displayName": "testsub"
  }
}

回应:

enter image description here

要解决该错误,您需要为该应用程序授予 API Management Service Contributor 角色,如下所示:

转到 Azure 门户 -> APIM 服务 -> 您的 APIM -> 访问控制 (IAM) -> 添加角色分配

enter image description here

授予上述角色后,我再次生成访问 token 并运行如下相同的查询:

PUT https://management.azure.com/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.ApiManagement/service/apimService1/subscriptions/testsub?api-version=2021-12-01

回应:

enter image description here

当我检查门户时,APIM 订阅已成功创建,如下所示:

enter image description here

引用:

How to use Role-Based Access Control in Azure API Management | Microsoft Docs

关于azure - Microsoft Azure OAuth 客户端凭据 token 获得 "AuthorizationFailed"响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73426165/

相关文章:

javascript - 如何在 React Native 或 expo 代码中存储敏感数据? (带钥匙串(keychain)和 key 库)

powershell - Get-AzureRmAppServicePlan 和 Get-AzureRmWebApp 在 Runbook 中返回异常

windows - 无法远程运行 powershell 脚本,导致 Azure 中的测试代理部署失败

azure - 使用 Azure Active Directory 用户时,远程 WMI 查询失败并显示 'A security package specific error occurred'

python - HTTPError 403(禁止),Django 和 python-social-auth 使用 OAuth2 连接到 Google

php - RewriteRule for url 导致 Facebook 登录中断

odata - 微软图: get only users from group

c# - Azure Active Directory 应用服务无法使用用户凭据/ token 连接到 Azure Keyvault

c# - 未找到范围 (scp) 中的 Azure AD 访问 token

azure - 当我以 Release模式发布时,为什么我会看到用于 .NET 启动的 azure function host 且 Debug=True?