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.