首页 > 解决方案 > 如何获取自定义 Azure App API 的有效访问令牌?(MSAL.js)

问题描述

我正在尝试构建一个小型 Web 应用程序,它可以显示我的姓名、日程安排和学校成绩。

我的学校主要使用微软的服务,这让我想到在我的项目中使用他们的 Azure API 端点(用于日程和成绩)。

我有权在 Azure 门户中创建应用程序注册,所以我这样做了,并且可以使用我的学生电子邮件登录。我还尝试获取 Microsoft Graph API,效果非常好。

但是,当我尝试获取 Grades 端点时,它会返回 401 Unauthorized 错误。我猜这与范围有关,但我不确定。事实证明,我的访问令牌对那些 API 端点无效。

所以我的问题是,我如何获得对这些 API 有效的访问令牌?或者甚至有可能吗?请记住,它们是 Azure 门户中的单独应用程序注册,我只能编辑我自己的应用程序,而不是我学校的应用程序注册。

这是我的 JavaScript 文件,带有一些注释:

const config = {
    auth: {
        clientId: "my_client_id_is_here",
        authority: "https://login.microsoftonline.com/my_tenant_id_is_here",
        redirectUri: "localhost"
    }
};

async function login() {

    console.log("Started..")

    var client = new Msal.UserAgentApplication(config);

    var request = {
        scopes: [ 'User.Read' ]
    };

    let loginResponse = await client.loginPopup(request);
    console.dir(loginResponse);

    let tokenResponse = await client.acquireTokenSilent(request);
    console.dir(tokenResponse);

    // User REQUEST - Here I fetch the Graph API for some profile information, which works fine and writes it to the HTML perfectly.

    let req = await fetch("https://graph.microsoft.com/v1.0/me/", {
        headers: {
            "Authorization": "Bearer " + tokenResponse.accessToken
        }
    });

    let json = await req.json();

    console.log(json);

    document.write("Logged in as " + json.displayName);
    document.write("<br>" + json.mail);
    document.write("<br>" + json.jobTitle + " " + json.officeLocation);

    // School Grades REQUEST - this is the part where I'm trying to fetch my School Grades, but it's not working since it gives me a 401 error..
    
    let gradesReq = await fetch("https://myschool.azurewebsites.net/API/Grades/GetGrades", {
        "headers": {
          "authorization": "Bearer " + tokenResponse.accessToken
        }
    });

    try {
        let gradesJson = await gradesReq.json();
        console.log(gradesJson);
    } catch (err) {
        document.write("An error occured while trying to get the school grades..")
    }
}```

标签: javascriptazuremsalmsal.js

解决方案


你的想法是对的。您收到此错误的原因是因为您将针对不同范围 ( User.Read) 获取的访问令牌与您的 API 一起使用。

修复相当简单。

您必须首先使用 Azure AD 保护您的 API。您可能会发现此链接有助于实现此功能:https ://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-overview 。

完成此操作后,您需要做的就是为您的 API 获取令牌。在这种情况下,您的作用域代码将类似于以下内容:

var request = {
    scopes: [ 'api://<your-application-id>/.default' ]
};

一旦您获得了此范围的令牌并将其与您的 API 一起使用,您应该不会收到您所获得的 401 异常。


推荐阅读