'Azure Ad B2C, AADSTS9002313: Invalid request. Request is malformed or invalid

As the title says, I have a problem get the access token back from the token API, I also tried using the method provided by the msal/node library.

I noticed something weird about the code I get from the redirect uri I provided, In the url I get client_info and not code image -

Taken from the redirect Uri

Im trying 3 different methods to fetch the tokens.

In the first one I get the error included in the title, in the other two I get the following error - ServerError: invalid_grant: undefined - [undefined]: AADB2C90090: The provided JWE is not a valid 5 segment token

My Code -

const getAuthentication = (azureConfig, azureAuthCode, confidentialClientApplication) => {
   //prepare the request for authentication    
   debugger    
   tokenRequest.code = azureAuthCode
   tokenRequest.grant_type = 'authorization_code';
   tokenRequest.scope = [`${azureConfig.b2c._scopes.ScheduleAppointments}`];
   tokenRequest.client_id = process.env.APP_CLIENT_ID;
   tokenRequest.client_secret = process.env.APP_CLIENT_SECRET,
   

   console.log(tokenRequest)

METHOD ONE -----

ERROR RETURNED - AADSTS9002313: Invalid request. Request is malformed or invalid

request({
    url: `https://login.microsoftonline.com/${process.env.TENANT_NAME}/oauth2/v2.0/token`,
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: tokenRequest,
}, (req, res) => {console.log(res)})

METHOD TWO -----

ERROR RETURNED - ServerError: invalid_grant: undefined - [undefined]: AADB2C90090: The provided JWE is not a valid 5 segment token

// try {
//  const response = await confidentialClientApplication.acquireTokenByCode(tokenRequest)
//  request.req.session.sessionParams = {user: response.account, idToken: response.idToken};
//  console.log("\nAuthToken: \n" + JSON.stringify(response));
//  res.render('getPatientAppointments',{showSignInButton: false, givenName: response.account.idTokenClaims.given_name});
//   } catch (error) {
//  console.log("\nErrorAtLogin: \n" + error);
//   }

METHOD THREE -----

ERROR RETURNED - ServerError: invalid_grant: undefined - [undefined]: AADB2C90090: The provided JWE is not a valid 5 segment token

//    confidentialClientApplication.acquireTokenByCode(tokenRequest).then((response)=>{
   
//    req.session.sessionParams = {user: response.account, idToken: response.idToken};
//    console.log("\nAuthToken: \n" + JSON.stringify(response));
//    res.render('getPatientAppointments',{showSignInButton: false, givenName: response.account.idTokenClaims.given_name});
//    }).catch((error)=>{
//     console.log("\nErrorAtLogin: \n" + error);
//    });

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source