Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 973
nortex_dev Avatar asked Feb 06 '20 10:02

nortex_dev


People also ask

How do I use OAuth2 with Discord?

Getting an OAuth2 URL Open your Discord applications open in new window, create or select an application, and head over to the "OAuth2" page. Once you've added your redirect URL, you will want to generate an OAuth2 URL. Lower down on the page, you can conveniently find an OAuth2 URL Generator provided by Discord.

What is Redirect_uri OAuth2?

redirect_uri. Required. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in your client's API Console Credentials page.

What is a Discord redirect?

This means the link with the scope guilds. join will be on a website that you own and once the user either grants and denies this, the grant page will redirect the client back to the website.

How do you get a Discord bearer token?

Open Developer Tools, then click Network. Press F5 on your keyboard to reload the page. Type /api into the Filter field, then click library. Click the Headers tab, then scroll down to authorization to find your Discord token.


2 Answers

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!

like image 80
heytulsiprasad Avatar answered Nov 15 '22 07:11

heytulsiprasad


I know this is resolved for a long time but i'm adding this in case someone has this issue and don't know what's happening: if you are calling the https://discord.com/api/oauth2/token endpoint to resolve a code to a token, you have to make the redirect_uri field the SAME as the one that generated the code with the https://discord.com/api/oauth2/authorize endpoint, otherwise you'll get this error. It also has to be, like mentioned above, EXACTLY (querystrings don't work) the same as one of the redirects in the redirects list of your application.

like image 25
Shyne Avatar answered Nov 15 '22 06:11

Shyne