Search code examples
node.jsexpressoauth-2.0discord

Issue with Discord OAuth2 redirect_uri component


I'm currently working on Discord OAuth2 client for my web application.

No matter how hard I try to set the redirect_uri to make discord not send the error, I can't get fix it, and I keep getting this message:

Invalid OAuth2 redirect_uri : You can now close this tab.

Screenshot (sorry for the foreign language in the second line)

My redirect_uri is:

const redirect = encodeURIComponent("http://localhost/callback");

(the website exists on the server and works as should.)

**I tried so many combinations, including: - adding https:// instead of http:// - adding .com to the domain - adding a slash after the word callback - using normal string instead of encodeURIComponent - generating the oauth2 redirect_uri via the Discord Developer page **

The only thing that worked was totally removing the redirect_uri from the URL parameters, which made the app work, but didn't redirect to the correct place afterward;

(This is my oauth2 url):

https://discordapp.com/api/oauth2/authorize?client_id=${id}&redirect_uri=${redirect}&response_type=code&scope=identify%20guilds%20email,

when id = client id in string type and redirect = (above) the url

Thanks for any help.

Edit 1: setting the redirect_uri intentionally to a wrong URL, sends a JSON message saying "Badly formatted redirect_uri." (not the one visible on the screenshot!)

Edit 2: example complete url: https://discordapp.com/oauth2/authorize?client_id=528972063096963140&redirect_uri=http%3A%2F%2Flocalhost%2Fdiscord%2Fcallback&response_type=code&scope=identify%20guilds%20email


Solution

  • I got this issue today and after debugging a while around, I found what I was doing wrong.

    While I registered my application on Discord, it asked me for a redirect_uri which we need to specify so that Discord can only allow those urls to redirect from the login page. But after fiddling a while on my project I changed the redirect url from node, which didn't match with what I already specified on Discord. That's why this issue popped up for me.

    Screenshot from my discord developer dashboard (change this redirect url):

    enter image description here

    Fix: Go to Discord and update your new redirect url (as per your need). And this will fix the error on the login page that you're facing. Remember to save either by pressing enter in the field or by pressing the save-button that pops up at the bottom of the page.

    Hope I could help!