我尝试使用Outlook的API,并且我使用simple-oauth2来授权权限。我从微软文档中获取了片段,但是我收到了以下错误:
Error getting token [object Object]
...
TypeError: Cannot read property 'send' of null at tokenReceieved
我有一个函数可以将我引导到Azure的登录页面:
app.get('/authorize', (req, res) => {
console.log("Request handler 'authorize' was called.");
const { code } = req.query;
// The authorization code is passed as a query parameter
console.log(`Code: ${code}`);
authHelper.getTokenFromCode(code, tokenReceived, res);
});
从单独的文件调用getTokenFromCode
:
function getTokenFromCode(auth_code, callback, response) {
var token;
oauth2.authorizationCode.getToken(
{
code: auth_code,
redirect_uri: redirectUri,
scope: scopes.join(' ')
},
function(error, result) {
if (error) {
console.log('Access token error: ', error.message);
callback(response, error, null);
} else {
token = oauth2.accessToken.create(result);
console.log('Token created: ', token.token);
callback(response, null, token);
}
}
);
}
但问题出在tokenRecieved
:
const tokenReceived = (req, res, error, token) => {
if (error) {
console.log(`Error getting token ${error}`);
res.send(`Error getting token: ${error}`);
} else {
req.session.access_token = token.token.access_token;
req.session.refresh_token = token.token.refresh_token;
res.redirect('/logincomplete');
}
};