Discord OAuth2 Redirect URI always says “Invalid OAuth2 redirect_uri”

Hi. I’m having trouble with the redirect_uri of Discord OAuth2 systems.
No matter what I type in the &redirect_uri= query, I always get presented with this screen:

No matter what URL I use, if it has a trailing / at the end if it has http:// or https://, if it leads to any certain route, even if I try to encode the redirect uri to an HTML format, it still doesn’t wanna work.
My current redirect uri is:
https://discordapp.com/oauth2/authorize?client_id=691011743249334333&permissions=8&scope=bot&guild_id=694885901700038656&redirect_uri=https://localhost/callback
although I have tried a million other ways before too, neither of which worked.
(Quick note: if you remove the redirect_uri query parameter, it works beautifully, but, expectedly, doesn’t redirect after success.)
Setting it in the OAuth2 tab in the Developer page doesn’t change anything.
I also want the redirect uri to be slightly different everytime, so that’s why I have to do it in the url, not the oauth2 tab.
Also, it doesn’t matter if the scope is “bot” or “email”, “guilds” etc., neither work.
This isn’t the first time I need to solve this issue, and I have to fix it.

This part is the problem, discord site cannot look at its localhost to find your server. Instead put your server endpoint that handles the redirect, example

&redirect_uri=https://mysite.glitch.me/callback
1 Like

Hi! Thanks for the response…

Even though I have localhost there, I tried one of my domains and it didn’t work. I also tried a domain that I know for sure works, e.g. google.com in my case.
Hope to solve the issue further.

Also, it would be helpful if you provided an example link for me that you are sure works. It doesn’t even have to be the right client id or the scope.

You may need the uri to be url-encoded, as it is passed as a query parameter inside another uri.

This can get confusing on web forums, as the encoded string is decoded by the browser so we can read it … here is an example:

https://discordapp.com/api/oauth2/authorize?client_id=7777777777777777777&redirect_uri=https%3A%2F%2Fexample-project.glitch.me%2Fredir&response_type=code&scope=identify

Hey, what is the URL you are using for the URI?

Because mishavee is right. It HAS to be your website because it has the proper scripts.

Discord gives that error if you didn’t add the URI in the OAuth section of your Application. Make sure the URI is added in the Application.

I didn’t know you have to add it to the redirect uris in the Discord Developer page, and when I did, the error disappeared. However, now nothing appears now. It works, but it doesn’t redirect the user, nor show the “Once you authenticated you will be redirected to blah blah”.
To address some questions:

  1. Yes, my URI is encoded using encodeURIComponent
  2. This keeps happening no matter if I use localhost, my own (verifiied to work) domain
    I think it’s also very worth noting that I’m using the “bot” scope in this case and not a normal “identify”, “email”, “guilds” etc. This may be causing problems.

Also, as a quick follow up question:
Can I somehow change the URL dynamically, so, for example, I can redirect to page.com/the guild id or page.com/callback?id=the guild id?
I could make a workaround for that, but it would be cool if I could do such a thing.
Currently, it’s impossible due to myself having to put it in on the dashboard.
It’s completely alright if I can’t though.

1 Like

You could use the state parameter for that:

Hi.

Could you provide me an example of a redirect uri that does this? It would be much appreciated.

I’ll try to find one, but the general flow for how i do it is like this:

  1. save in database or something an entry with a randomly generated string of characters as the key and the path to redirect as the value
  2. add that string to the oauth url like so: `state=thatstringofcharacters"
  3. When you get your callback, get the state query from the url and use that as your key to check the database and see where you need to send the user

Oh, ok. Gotcha.

I will just do it using MongoDB, it has a great id’s system.

1 Like

Alright, cool. I’ve used quick.db and mongoose for it, and they both work well. Best of luck! :slight_smile:

Lol Hi! I finally found you…