401未经授权-调用启用了AD身份验证的Azure API

问题描述 投票:0回答:1

我在Azure中托管了一个具有AD身份验证的API。在浏览URL时,它要求进行身份验证,并且在输入凭据时,API可以正常工作。但是,当使用具有基本身份验证和Oauth2身份验证方法的Get方法在Postman应用程序中调用API时,会出现401未经授权的错误。

已获取客户端ID,密钥,租户ID。

广告应用程序中的APP URI与网络应用程序网址相同,但不以“ /”结尾。已授予广告应用中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
1个回答
0
投票

获取访问令牌的参数不正确。由于要使用v1.0端点访问Web应用程序,因此应使用resource参数而不是scope,并且该值应为相同的client_id。另外,您不应该在前端使用client credential flow

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.