【发布时间】:2020-02-28 23:25:57
【问题描述】:
我正在使用 '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 解析失败,错误代码:80049217
任何建议我做错了什么???
更新: 这些是我使用的范围:'openid,profile,offline_access,calendars.read'
更新: 稍微编辑范围后,现在我收到以下错误:无效的受众。
解码在 jwt.ms 收到的令牌时,这是“aud”的值:“00000002-0000-0000-c000-000000000000”
passport-azure-ad-oauth2 是否是用于检索 MS Graph API 令牌的错误库?
【问题讨论】:
-
根据我的错误信息,能否请您抓住
process.env['OUTLOOK_ACCESS_TOKEN'] = accesstoken;打印您的访问令牌? -
原来,passport-azure-ad-oauth2 为旧的 Azure AD Graph API 提供了访问令牌。所以我切换到 passport-microsoft,它为新的 Microsoft Graph API 检索令牌
标签: node.js microsoft-graph-api auth0 adal microsoft-graph-calendar