繁体   English   中英

如何获取自定义 Azure App API 的有效访问令牌? (MSAL.js)

[英]How to get a valid access token for a custom Azure App API? (MSAL.js)

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

我的学校主要使用 Microsoft 的服务,这让我想到在我的项目中使用他们的 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..")
    }
}```

你的想法是对的。 您收到此错误的原因是因为您正在使用为不同范围 ( 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 异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM