1

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...

Adam Ch.
  • 163
  • 1
  • 13

0 Answers0