Keyvault 身份验证(REST API)

Keyvault Authentication (REST API)

微软零散的文档让我有点困惑。

我已经创建了一个应用程序(https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal),这意味着我现在有:

我已进入 Azure 门户中的 KeyVault,并已授予应用程序权限。

出于测试目的,我正在尝试 运行 通过 CURL 进行测试。我为此使用的基础是以下 Microsoft 页面 (https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#request-an-access-token and https://docs.microsoft.com/en-us/rest/api/#create-the-request)

所以,我做的第一件事就是通过以下调用获取令牌:

curl -d "grant_type=client_credentials&client_id=<removed_for_security>&client_secret=<removed_for_security>" https://login.microsoftonline.com/<removed_for_security>/oauth2/token

这个returns一个令牌。

然后我(尝试)按如下方式使用该令牌:

curl -H "Authorization: Bearer <removed_for_security>” -vv https://<removed_for_security>.vault.azure.net/secrets/<removed_for_security>/<removed_for_security>

我没有得到任何内容,只有 "HTTP/1.1 401 Unauthorized"

好的,所以我可以确认你所做的请求是有效的,在大多数情况下,你忘记了 API 版本,但问题不在于 API 版本(它会告诉你的)。

https://xxx.vault.azure.net/secrets/xxx/?api-version=2015-06-01

这个url有效,所以我猜令牌不对。最简单的检查方法是转到 JWT.io 并将令牌粘贴到那里并查看内容,如果它们与 Key Vault 期望的内容匹配。可能你不匹配。

您需要指定为其请求令牌的资源。

curl -d "grant_type=client_credentials&client_id=<removed_for_security>&client_secret=<removed_for_security>&resource=https://vault.azure.net" https://login.microsoftonline.com/<removed_for_security>/oauth2/token

并添加 api 版本。