【发布时间】:2022-01-12 05:41:33
【问题描述】:
我正在尝试获取用户令牌以将一个驱动器集成到我正在构建的 APP 中
我首先得到 auth URL,这里是端点实现
const pca = new msal.ConfidentialClientApplication(config);
exports.getAuthUrl = (req, res) => {
const authCodeUrlParameters = {
scopes: ['user.read', 'files.readwrite', 'offline_access'],
redirectUri: 'https://localhost:8080/onedrive',
};
// get url to sign user in and consent to scopes needed for application
pca
.getAuthCodeUrl(authCodeUrlParameters)
.then((response) => {
res.status(200).send(response);
})
.catch((error) => console.log(JSON.stringify(error)));
};
然后使用我在客户端身份验证成功后返回的代码作为参数传递到第二个端点以获取令牌
exports.getToken = (req, res) => {
const tokenRequest = {
code: req.query.code,
scopes: ['user.read', 'files.readwrite', 'offline_access'],
redirectUri: 'https://localhost:8080/onedrive',
};
pca
.acquireTokenByCode(tokenRequest)
.then((response) => {
console.log('\nResponse: \n:', response);
res.status(200).send(response);
})
.catch((error) => {
console.log(error);
res.status(500).send(error);
});
};
因此,正如官方文档中所述,如果您添加了 offline_access 范围,您将获得一个刷新令牌
有人有这方面的经验吗?我用了这两个库,部分代码微软已经提供了const msal = require('@azure/msal-node');
【问题讨论】: