Unable to exchange auth-code to get access-token using Microsoft identity platform in my node application

不羁的心 提交于 2021-02-08 08:16:22

问题


I am implementing OAuth2 using Microsoft identity platform. For this purpose, I registered my app to Azure Active Directory admin center and got an app-id and other app credentials. Redirect-url also mentioned as well.

So first of all, I generated auth-url (code snippet is provided below) and got an auth-code to my redirect endpoint. when I try to exchange auth-code to get access-token, it throws as exception with statuscode 401 with error message "401 Unauthorized".

I am using simple-oauth2 node package. Here is my snippet to generate auth-url

function authUrl() {
    const url = oauth2.authorizationCode.authorizeURL({
        redirect_uri: outlookCalendar.REDIRECT_URI,
        scope: outlookCalendar.APP_SCOPES
    });
    return url;
}

where APP_SCOPES = "User.Read https://outlook.office.com/Calendars.ReadWrite"

here is auth-url generated by above code

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&client_id=xxxxxxxxxxxx&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcalendars%2Foutlook%2Fcode&scope=User.Read%20https%3A%2F%2Foutlook.office.com%2FCalendars.ReadWrite

here is the function to get access-token

async function accessToken(code) {
    try {
        const result = await oauth2.authorizationCode.getToken({
            code : code,
            redirect_uri: outlookCalendar.REDIRECT_URI,
            scope: outlookCalendar.APP_SCOPES
        });

        const token = oauth2.accessToken.create(result);
        console.log('Token created: ', token.token);

        return { tokens: token.token.access_token };
    } catch (exception) {
        console.log(exception);
        return { error: exception };
    }
}

So, Please anyone tell me the way to get access token or something was going wrong in my code. Thank you...

来源:https://stackoverflow.com/questions/56050644/unable-to-exchange-auth-code-to-get-access-token-using-microsoft-identity-platfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!