我正在使用'passport-azure-ad-oauth2' npm 模块来获取访问 token ,然后我可以将其传递给 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 解析失败,错误代码:80049217
有什么建议我做错了什么吗???
更新: 这些是我正在使用的范围:'openid,profile,offline_access,calendars.read'
更新: 对范围进行一些编辑后,现在我收到以下错误:无效受众。
在解码 jwt.ms 收到的 token 时,这是“aud”的值:“00000002-0000-0000-c000-000000000000”
passport-azure-ad-oauth2是否是用于检索 MS Graph API token 的错误库?
最佳答案
原来有一个用于 microsoft-graph api 的护照库:passport-microsoft
我使用了该软件包中的 MicrosoftStrategy,一切似乎都工作正常。
passport-azure-ad-oauth2 适用于旧版 Azure AD Graph API,而 passport-microsoft 适用于新版 Microsoft Graph API
关于node.js - CompactToken 解析失败,错误代码 : 80049217 when using passport library to access Microsoft Graph API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679990/