使用 azure 活动目录 v1 连接登录后,Azure 使用详细信息 API 显示 "Authentication failed"

Azure usage details API shows "Authentication failed" after sign in with azure active directory v1 connection

我完全遵循了这个 link https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=aadv1%2Ccsharp%2Cbot-oauth 并创建了 Azure AD 应用程序注册并为我的网络应用程序机器人使用了 Azure Active Directory v1。

登录后,我查看了令牌,但使用该令牌我无法访问 Azure API,如下面在 Postman 中的响应所示:

{
"error": {
    "code": "AuthenticationFailed",
    "message": "Authentication failed."
}

我在下面调用了 Azure API:

https://management.azure.com/subscriptions/${subscriptionId}/providers/Microsoft.Consumption/usageDetailsapi-version=2018-10-01

在我的 Azure AD 应用程序注册中,我已授予这些权限以访问 Azure API:

在我的 Web App Bot -> 设置 -> OAuth 连接设置中,我 select:

ClientId -> My application client id
ClinetSecret -> My application client secret 
GrantType -> I does not know what to give so I just typed "authorization_code" (If this wrong then Where I need to find my grantType) 
LoginURL -> https://login.microsoftonline.com 
TenantId -> common (To allow any user) 
ResourceURL -> https://graph.microsoft.com/ 
Scopes -> I just left blank

为什么我无法使用该令牌访问 Azure API?

任何帮助。谢谢

Azure AD 颁发的访问令牌将始终用于特定资源。令牌用于哪种服务在令牌的 "audience"(在 aud 声明中)中标识。使用 v1 端点时,应用请求访问令牌的资源在授权请求的 resource 参数中标识。在 v2 端点中,资源被标识为 scope 参数的一部分。

在你的例子中,你为你的机器人配置的资源是 Microsoft Graph (https://graph.microsoft.com),但是你试图使用生成的令牌来调用 Azure 管理 API。 Azure 管理 API 做的第一件事是检查它收到的访问令牌是否真的适用于它。如果观众不匹配,它会立即响应错误。

您无需尝试获取 Microsoft Graph 的令牌,而是需要配置您的机器人以获取 Azure 管理的令牌 API。您应该使用 https://management.azure.com,这是 Azure Management API 的资源 URI,而不是 https://graph.microsoft.com,这是 Microsoft Graph 的资源 URI。