DiscordAPIError - Invalid code in request

i'm getting this error sometimes when trying to exchange a token on /oauth2/token after logging in with oauth:
DiscordAPIError[invalid_request]: Invalid "code" in request.
DiscordAPIError[invalid_request]: Invalid "code" in request.
the weird thing: its working most of the time, which is also why i cant really reproduce it. my callback looks like this:
app.get('/callback', async (req, res) => {
if (!req.query.code) { return res.redirect('/') }

const body = new URLSearchParams({ 'client_id': appId, 'client_secret': clientSecret, 'code': req.query.code, 'grant_type': 'authorization_code', 'redirect_uri': `${host}/callback` })
const token = await client.rest.post(Routes.oauth2TokenExchange(), { body: body, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, auth: false, passThroughBody: true })
...
app.get('/callback', async (req, res) => {
if (!req.query.code) { return res.redirect('/') }

const body = new URLSearchParams({ 'client_id': appId, 'client_secret': clientSecret, 'code': req.query.code, 'grant_type': 'authorization_code', 'redirect_uri': `${host}/callback` })
const token = await client.rest.post(Routes.oauth2TokenExchange(), { body: body, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, auth: false, passThroughBody: true })
...
which means the request shouldnt even be made when there is no code. /callback is also the direct redirect_uri, which means the request there should only have valid codes directly from discord any ideas here?
4 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Meridian
MeridianOP3y ago
already did, i just thought maybe someone here could help me out. this is the most active discord development help server i know of, after all
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server