我正在使用“passport-azure-ad-oauth2” npm 模块来获取访问令牌,然后我可以将其传递给 MS Graph API。
passport.use(new AzureAdOAuth2Strategy({
clientID: process.env.OUTLOOK_CLIENT_ID,
clientSecret: process.env.OUTLOOK_SECRET,
callbackURL: '/auth/outlook/callback',
},
function (accesstoken: any, refresh_token: any, params: any, profile, done) {
logger.info('Completed azure sign in for : ' + JSON.stringify(profile));
logger.info('Parameters returned: ' + JSON.stringify(params));
const decodedIdToken: any = jwt.decode(params.id_token);
logger.info('Outlook Access Token:' + accesstoken);
logger.info('Decoded Token: ' + JSON.stringify(decodedIdToken, null, 2));
process.env['OUTLOOK_ACCESS_TOKEN'] = accesstoken;
// add new user with token or update user's token here, in the database
}));
然后,使用'@microsoft/microsoft-graph-client' npm 模块,从 Graph API 获取日历事件,如下所示:
try {
const client = this.getAuthenticatedClient(process.env['OUTLOOK_ACCESS_TOKEN']);
const resultSet = await client
.api('users/' + userId + '/calendar/events')
.select('subject,organizer,start,end')
.get();
logger.info(JSON.stringify(resultSet, null, 2));
} catch (err) {
logger.error(err);
}
getAuthenticatedClient(accessToken) {
logger.info('Using accestoken for initialising Graph Client: ' + accessToken);
const client = Client.init({
// Use the provided access token to authenticate requests
authProvider: (done) => {
done(null, accessToken);
}
});
return client;
}
但是,使用成功登录时提供的 accessToken,我收到以下错误: CompactToken parsing failed with error code: 80049217
任何建议我做错了什么???
更新:这些是我使用的范围:'openid,profile,offline_access,calendars.read'
更新:稍微编辑范围后,现在我收到以下错误:无效的受众。
在解码在 jwt.ms 收到的令牌时,这是“aud”的值:“00000002-0000-0000-c000-000000000000”
是不是passport-azure-ad-oauth2是用于检索 MS Graph API 令牌的错误库?