Search code examples
node.jsoauth-2.0oauthdiscorddiscord.js

Discord OAuth returns missing "code in request


I'm trying to use a simple example to fetch an access token from the Discord API. I have the authorization code, but I'm struggling on exchanging it for the access token.

My code so far:

const code = **my authorization code**;
const CLIENT_ID = **my client id**;
const CLIENT_SECRET = **my client secret**;
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
  {
    method: "POST",
    headers: {
      Authorization: `Basic ${creds}`,
  },
})
  .then((res) => res.json())
  .then((body) => console.log(body));

However, when I run this code, I get the following error:

{
   error: 'invalid_request',
   error_description: 'Missing "code" in request.'
}

Solution

  • I know how to fix this. So Discord API has been updated, you can no longer specify the code, grant type & redirect URI in the URL. You need to remove it from the URL and put it in the body.

    This should fix it: https://sourceb.in/BGWsbUzope