I'm in the process of developing an authentication application, but I keep encountering the error message
Invalid "code" in request
when attempting to obtain a refresh token from the code provided by Discord.
Below is a snippet of my request:
const response = await fetch(`https://discord.com/api/oauth2/token`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams({
grant_type: "authorization_code",
code: code,
redirect_uri: redirectURI,
client_id: process.env.DISCORD_CLIENT_ID as string,
client_secret: process.env.DISCORD_CLIENT_SECRET as string,
scope: 'identify email'
}).toString()
})
I have cross-checked that I am using the correct scopes, redirect URI, and code. My credentials are accurate, yet the JSON response indicates an error:
{ error: 'invalid_grant', error_description: 'Invalid "code" in request.' }
Could you please provide a solution to this issue? Thank you very much.