【发布时间】:2020-04-02 07:31:02
【问题描述】:
我有一个托管在 Azure 中的 API,启用了 AD 身份验证。浏览 URL 时,它要求进行身份验证,并且在输入凭据时,API 工作正常。但是在 Postman 应用程序中使用带有基本身份验证和 Oauth2 身份验证方法的 Get 方法调用 API 时,会出现 401 未经授权的错误。
已获取客户端 ID、密钥、租户 ID。
广告应用中的APP URI与网页应用Url相同,不以“/”结尾。 已授予广告应用中 API 的登录和读取权限。
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://login.microsoftonline.com/tenantID/oauth2/token",
"method": "POST",
"headers": {
"Accept": "*/*",
"Cache-Control": "no-cache",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id ": "********************",
"client_secret": "********************",
"scope ": "https://graph.windows.net"
},
success: function (response) {
console.log(response);
token = response.access_token;
$.ajax({
url: "https://azure.azurewebsites.net/api/example",
type: "GET",
"async": true,
"crossDomain": true,
dataType: "json",
useDefaultXhrHeader: false,
headers: {
"Accept": "*/*",
"Cache-Control": "no-cache",
"content-Type": "application/json;odata=verbose",
"authorization": "Bearer " + response.access_token
},
success: function (data) {
console.log(data);
}
});
}
});
在为 Web 应用程序配置 AD 应用程序时,我是否遗漏了什么?上面的代码还用于获取访问令牌并使用该访问令牌调用 API。 任何帮助深表感谢。
提前致谢
【问题讨论】:
-
如果您不分享您的代码,我们将如何调查您的问题?如果可能,请在您的问题中包含您的代码。
-
如果我的回答有帮助,请接受它作为答案(单击答案旁边的复选标记将其从灰色切换为已填充)。谢谢。
标签: azure azure-api-apps