Search code examples
javascriptoauth-2.0discord

"invalid_grant" error while trying to use Discord's oauth2


const response = await fetch(`https://discordapp.com/api/oauth2/token`, {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: JSON.stringify({
    client_id: process.env.CLIENT_ID,
    client_secret: process.env.CLIENT_SECRET,
    redirect_uri: redirectURL,
    grant_type: 'authorization_code',
    code: code,
  }),
});

When I run the above code, it returns with { error: 'invalid_grant' } and I do not know why. Are there any fixes to this? Thank you.


Solution

  • I fixed it, all I had to do was to use querystring instead of JSON.