- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个小型网络应用程序,它可以显示我的名字、我的日程安排和我的学校成绩。
我的学校主要使用 Microsoft 的服务,这让我想到在我的项目中使用他们的 Azure API 端点(用于时间表和成绩)。
我有权在 Azure 门户中创建应用程序注册,因此我这样做了,并让它可以使用我的学生电子邮件登录。我还尝试获取 Microsoft Graph API,效果非常好。
但是,当我尝试获取成绩端点时,它返回 401 未经授权错误。我猜这与范围有关,但我不确定。事实证明,我的访问 token 对于这些 API 端点无效。
所以我的问题是,如何获得对这些 API 有效的访问 token ?或者说有可能吗? 请记住,它们是 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..")
}
}```
最佳答案
你的想法是正确的。您收到此错误的原因是您在 API 中使用了为不同范围 (User.Read
) 获取的访问 token 。
修复相当简单。
您要做的就是首先使用 Azure AD 保护您的 API。您可能会发现此链接有助于实现此功能:https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-overview .
完成此操作后,您所需要做的就是获取 API 的 token 。在这种情况下,您的范围代码将类似于以下内容:
var request = {
scopes: [ 'api://<your-application-id>/.default' ]
};
一旦您获取此范围的 token 并将其与您的 API 一起使用,您就不应该收到您所收到的 401 异常。
关于javascript - 如何获取自定义 Azure 应用程序 API 的有效访问 token ? (MSAL.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69562175/
我从 GitHub 下载了示例以试验 Azure AD B2C https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-
为了在我的 JavaScript 代码中使用 MSAL 获取访问 token ,我拼凑了大量教程和文档。这是我的研究结果。 最佳答案 npm 安装@azure/msal 从@azure/msal 导入
我正在编写一个 Chrome 扩展程序,它需要访问需要 AAD 访问 token 的 ASP.NET Core Web API。我正在尝试使用 msal NPM 包提供身份验证流程,以便用户可以使用他
我没有从使用 MSAL 库的 UserAgentApplication 实例的 loginPopup(requestObj) 获得 loginResponse。 Azure 示例用作 https://
我对 OAuth2 以及 AccessToken 和 RefreshToken 的概念比较熟悉。 看起来 MSAL 在使用 ClientApplicationBase.AcquireTokenSile
我使用了 msal.js 文件(版本 1.0.3),但是没有调用 authCallback 函数, var clientApplication = new Msal.UserAgentApplicat
Dynamics 365 的门户功能提供了使用 Azure AD B2C 登录的功能。这是我对其控制能力有限的产品功能,据我所知,该功能是在没有 MSAL 的情况下实现的。 在门户上下文中,我尝试使用
我正在尝试遵循 Microsoft active-directory 的教程 将客户端 ID 和 key 输入代码并运行代码后,它会将我带到登录页面,我使用凭据登录,但出现以下错误: "errorCo
我正在尝试遵循 Microsoft active-directory 的教程 将客户端 ID 和 key 输入代码并运行代码后,它会将我带到登录页面,我使用凭据登录,但出现以下错误: "errorCo
我正在尝试使用 Azure MSAL 浏览器 js CDN 将对象初始化为 PublicClientApplication。但我发现 Msal 未定义。 CDN:https://alcdn.msaut
我尝试使用此处提供的代码:https://github.com/Azure-Samples/active-directory-javascript-singlepageapp-dotnet-webap
@azure/msal-browser 2.14.1@azure/msal-react 1.0.0-beta.2 我们正在使用自定义 B2C 政策 当我使用 PublicClientApplicati
我有一个使用 msal 0.1.2 的 angular7 应用程序。用户间歇性地无法访问任何需要身份验证的内容,当您查看开发人员工具网络选项卡时,您可以看到一个 GET 请求来获取 https://l
@azure/msal-browser 2.14.1@azure/msal-react 1.0.0-beta.2 我们正在使用自定义 B2C 政策 当我使用 PublicClientApplicati
我想使用应用程序 ID 和 secret 对 AAD 用户进行身份验证,以通过 MSAL 访问 powerBi 资源。所以我想获取访问 token 并将其缓存在 SQL Db 中。 经历了 docum
我尝试包含 Azure AD 进行登录,但在运行 npm install @azure/msal-browser @azure/msal-angular 以及 npm install @azure/m
我尝试包含 Azure AD 进行登录,但在运行 npm install @azure/msal-browser @azure/msal-angular 以及 npm install @azure/m
我想安装 @azure/msal-angular 以使用 Angular 作为前端来使用 azure B2C 身份验证,但是当我尝试安装该包时,它给了我一个错误 npm ERR! code E
我想安装 @azure/msal-angular 以使用 Angular 作为前端来使用 azure B2C 身份验证,但是当我尝试安装该包时,它给了我一个错误 npm ERR! code E
使用msal.js库(Microsoft身份验证库),可以通过哪种方法知道给定用户是否已经登录?我的意图是如果用户的凭据已经保存在浏览器的存储中,则避免显示登录弹出窗口 我目前的做法: functio
我是一名优秀的程序员,十分优秀!