1

我完全按照这个链接https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=aadv1%2Ccsharp%2Cbot-oauth和为我的 Web 应用程序机器人创建了 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 连接设置中,我选择:

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?

任何帮助。谢谢

4

1 回答 1

1

Azure AD 颁发的访问令牌将始终用于特定资源。令牌的“受众”(在声明中)中标识了令牌所针对的服务aud。使用 v1 端点时,应用程序请求访问令牌的资源resource在授权请求的参数中标识。在 v2 端点中,资源被标识为scope参数的一部分。

在您的情况下,您已将机器人配置为获取令牌的资源是 Microsoft Graph ( https://graph.microsoft.com),但随后您尝试使用生成的令牌来调用 Azure 管理 API。Azure 管理 API 做的第一件事是检查它收到的访问令牌是否真的是为它准备的。如果受众不匹配,它将立即以错误响应。

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

于 2019-06-26T12:26:01.197 回答