Skip to content

Instantly share code, notes, and snippets.

@TheOnlyWayUp
Created April 14, 2024 13:32
Show Gist options
  • Save TheOnlyWayUp/509cdabf6b06aa803425f3b763fb3afa to your computer and use it in GitHub Desktop.
Save TheOnlyWayUp/509cdabf6b06aa803425f3b763fb3afa to your computer and use it in GitHub Desktop.
Discord OAuth2 - Exchange code to access token. Fixes Invalid Code error
let code = ...
let DISCORD_CLIENT_ID;
let DISCORD_CLIENT_SECRET;
let DISCORD_REDIRECT_URI;
let code_exchange = await fetch("https://discordapp.com/api/oauth2/token", {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
'method': 'POST',
body: new URLSearchParams({
client_id: DISCORD_CLIENT_ID,
client_secret: DISCORD_CLIENT_SECRET,
grant_type: 'authorization_code',
redirect_uri: DISCORD_REDIRECT_URI,
code: code,
})
})
// Thanks https://stackoverflow.com/a/58437909/
// Thanks https://msyyn.medium.com/how-to-add-discord-oauth-to-your-sveltekit-web-application-7c2e69d7656e
console.log(await code_exchange.json();)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment