【发布时间】:2021-12-02 23:12:08
【问题描述】:
我将大量教程和文档拼凑在一起,以便在我的 JavaScript 代码中使用 MSAL 获取访问令牌。这是我的研究结果。
【问题讨论】:
标签: msal
我将大量教程和文档拼凑在一起,以便在我的 JavaScript 代码中使用 MSAL 获取访问令牌。这是我的研究结果。
【问题讨论】:
标签: msal
npm install @azure/msal
从@azure/msal 导入必要的类
import {
UserAgentApplication,
AuthenticationParameters,
Configuration,
} from "@azure/msal";
制作 msal 对象
const config: Configuration = {
auth: {
clientId: <client id - your app's client id>,
authority: `https://login.microsoftonline.com/<tenantid>`,
redirectUri: <the redirect Uri>,
},
};
const params: AuthenticationParameters = {
authority: `https://login.microsoftonline.com/${Tenantid}`,
scopes: [`${AppIDUri}/user_impersonation`], <-- the API that you're trying to call
};
const myMSAL = new UserAgentApplication(config);
获取访问令牌
try {
const login = await myMSAL.acquireTokenSilent(params);
return login.accessToken;
} catch (error) {
await myMSAL.loginPopup(params);
const login = await myMSAL.acquireTokenSilent(params);
return login.accessToken;
}
参考资料:
https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-acquire-cache-tokens
Azure/Msal authentication inside PowerApp Component Framework returns AADSTS50177 error
【讨论】: