79230810

Date: 2024-11-27 15:08:29
Score: 2.5
Natty:
Report link

I finally get the token by using URLSearchParams and set again application/x-www-form-urlencoded instead of using JSON.stringify with application/json. I don't undestand why with application/json I got an Invalid request : Missing form parameter: grant_type that I was correctly set in the body.

Here is my final code to retrieve the token :

export default defineEventHandler(async (event) => {
  try {
    const token  = await fetch(`https://****/realms/test/protocol/openid-connect/token`,
      { 
        method : 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: new URLSearchParams({
          'client_id': "****",
          'client_secret': "****",
          'grant_type': "password",
          'username': "****",
          'password': "****"
        })
      });

    return token;
  } catch (error) {
    sendError(event, createError({
      statusCode: 500,
      statusMessage: 'Internal Server Error: ',
      data: {
          error: error.message,
          stack: error.stack,
      }
    }));
  }

})

Thanks to @C3roe to help me to understand my problem.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @C3roe
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Wasabi-sudo