我将大量教程和文档拼接在一起,以便在我的 JavaScript 代码中使用 MSAL 获取访问令牌。这是我的研究结果。
npm 安装@azure/msal
从 @azure/msal 导入必要的类
import {
UserAgentApplication,
AuthenticationParameters,
Configuration,
} from "@azure/msal";
Run Code Online (Sandbox Code Playgroud)
制作 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);
Run Code Online (Sandbox Code Playgroud)
获取访问令牌
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;
}
Run Code Online (Sandbox Code Playgroud)
参考:
https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-acquire-cache-tokens
PowerApp 组件框架内的 Azure/Msal 身份验证返回 AADSTS50177 错误
| 归档时间: |
|
| 查看次数: |
28253 次 |
| 最近记录: |