0

I am using next-auth in nextjs with keycloak as provider. The code snippet looks as follows:

const url = `http://localhost:8080/realms/<realm-name>/protocol/openid-connect/token`;
const keycloakParams = new URLSearchParams();
keycloakParams.append('client_id', process.env.KEYCLOAK_CLIENT_ID!);
keycloakParams.append('client_secret',process.env.KEYCLOAK_CLIENT_SECRET!);
keycloakParams.append('grant_type','refresh_token');
keycloakParams.append('refresh_token',<refreshToken>);
            
const TokenResponse = await axios.post(
                url,
                keycloakParams,
                {
                    headers: { "Content-Type": "application/x-www-form-urlencoded" }
                }
);
            
return response.json(TokenResponse.data);

This API call doesn't work, not sure what is the issue here.

I expected this API call to send the refresh tokens, and this works through postman. But this call is not working through javascript code

Nikita
  • 1
  • "works in postman" sounds like a CORS problem. This can have many reasons. I suggest to search for e.g. ["stackoverflow fetch localhost works in postman"](https://www.ecosia.org/search?q=stackoverflow+fetch++localhost+works+in+postman), or "CORS". E.g. [fetch-call-works-in-postman-but-not-localhost3000](https://stackoverflow.com/questions/69056466/fetch-call-works-in-postman-but-not-localhost3000). Check `Access-Control-Allow-Origin` header. check if you have script-blocker plugins. – kca Mar 29 '23 at 20:10
  • Thank you for your input. Yes, it was a CORS problem only. – Nikita Apr 04 '23 at 15:35

0 Answers0