我想针对Azure进行身份验证以声明访问令牌并将其用于Graph API。所以我完成了作业,并从本指南开始]
https://docs.microsoft.com/en-us/graph/auth-v2-service
我能够成功进行身份验证。但是代码量很大。这是我为身份验证编写的代码:
如果脚本需要访问当前会话令牌,则只需调用getAuthenticationInfo
函数。由于大量的代码,我想知道我的代码是多余的还是不正确的。
一些研究使我进入了[[MSAL.js]],(我认为)它为我处理了身份验证工作。 MSAL使用的方法不同于我上面发布的链接中的指南。我从这个普通的JS演示代码开始(我只有一个演示帐户,因此凭据不重要)const msal = require('msal');
async function start() {
const clientId = 'bec52b71-dc94-4577-9f8d-b8536ed0e73d';
const clientSecret = 'OV/NkBIWH7d3G/BGyJQN3vxQA]fT6qK@';
const tenant = '2c1714e1-1030-4da9-af5e-59630d7fe05f';
const scope = 'https://graph.microsoft.com/.default';
const grantType = 'client_credentials';
const msalConfig = {
auth: {
clientId
}
};
const msalInstance = new msal.UserAgentApplication(msalConfig);
const tokenRequest = {
scope,
};
try {
const tokenResponse = await msalInstance.acquireTokenSilent(tokenRequest);
const { accessToken } = tokenResponse;
console.log(accessToken);
} catch (error) {
throw error;
}
}
start();
我想知道我的登录凭据应该放在哪里。但是,当我运行代码时,出现以下错误
ReferenceError:未定义窗口似乎该代码试图访问浏览器存储。但是我的代码在Node环境中运行。因此,有人会很高兴告诉我如何从基于MSAL方法的第一个链接的代码段传输代码吗?
据我所知,MSAL应该为我解决问题,我不需要自己编写身份验证代码,并且可以缩短我自己的代码。
提前感谢!
我想针对Azure进行身份验证以声明访问令牌并将其用于Graph API。因此,我完成了家庭作业,并从本指南开始https://docs.microsoft.com/zh-cn/graph/auth-v2 -...