我无法获得足够的权限来使用我的应用程序(而不是通过用户登录)访问 Azure Key Vault。这是我的设置:
我已向名为“KeyVault”的应用程序授予所有权限。
我的应用程序已注册到 Azure Active Directory。我已授予其访问 Key Vault 的权限:
完成这一切之后,我尝试使用以下 REST API 获取访问 token :
https://login.microsoftonline.com/<DOMAIN_ID>/oauth2/token
client_id 和资源都是我之前在 Active Directory 中注册的应用程序的应用程序 ID。 (这是正确的吗?)
我确实得到了一个访问 token ,我用它来尝试查询我的保管库中的 secret 。不幸的是,正如您所看到的,它返回 401 错误,即没有权限。我做错了什么?
URL 来自 key 保管库中 key 的“ secret 标识符”。
最佳答案
The client_id and resource are both the App ID of my registered app in Active Directory I showed earlier.
不,资源 ID 不是应用程序 ID。正如 Rich 所说,该值为 https://vault.azure.net
。
我在实验室中使用 Power Shell 对其进行了测试,代码应如下所示:
$TENANTID=""
$APPID=""
$PASSWORD=""
$result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://vault.azure.net"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
$token=$result.access_token
$url="https://shui.vault.azure.net/secrets/shui01/cea20d376aee4d25a2d714df19314c26?api-version=2016-10-01"
$Headers=@{
'authorization'="Bearer $token"
}
Invoke-RestMethod -Uri $url -Headers $Headers -Method GET
注意:如果您想获取 API 输入信息,可以使用 Azure Power Shell -debug
来获取。例如:
关于azure - 通过 REST 使用服务到服务访问 token 访问 Azure Key Vault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48411755/