使用护照库访问 Microsoft Graph API 时,CompactToken 解析失败,错误代码:80049217

2024-05-06

我在用'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

有什么建议我做错了什么吗???

UPDATE: 这些是我正在使用的范围:'openid,profile,offline_access,calendars.read'

UPDATE: 对范围进行一些编辑后,现在我收到以下错误:无效受众。

在解码 jwt.ms 收到的令牌时,这是“aud”的值:“00000002-0000-0000-c000-000000000000”

是不是这样护照-azure-ad-oauth2用于检索 MS Graph API 令牌的库是否错误?


原来有一个 microsoft-graph api 的护照库:护照-微软

我使用了该包中的 MicrosoftStrategy,一切似乎都工作正常。

护照-azure-ad-oauth2适用于旧的 Azure AD Graph API,而护照-微软适用于新的 Microsoft Graph API

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用护照库访问 Microsoft Graph API 时,CompactToken 解析失败,错误代码:80049217 的相关文章

随机推荐