Skip to content

Instantly share code, notes, and snippets.

@xDimGG
Created October 15, 2017 19:07
Show Gist options
  • Save xDimGG/38e17b78f4e070c317e603c06cecbb33 to your computer and use it in GitHub Desktop.
Save xDimGG/38e17b78f4e070c317e603c06cecbb33 to your computer and use it in GitHub Desktop.
Basic Discord OAuth2 Example
const { get, post } = require('snekfetch');
const express = require('express');
const btoa = require('btoa');
const app = express();
const cfg = {
id: 'get_one',
secret: 'get_one'
};
app.get('/', (req, res) => {
res.redirect([
'https://discordapp.com/oauth2/authorize',
`?client_id=${cfg.id}`,
'&scope=identify+guilds',
'&response_type=code',
`&callback_uri=http://localhost:8080/authorize`
].join(''));
});
app.get('/authorize', (req, res) => {
const code = req.query.code;
const cred = btoa(`${cfg.id}:${cfg.secret}`);
post(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}`)
.set('Authorization', `Basic ${cred}`)
.then(response => res.redirect(`/guilds?token=${response.body.access_token}`))
.catch(console.error);
});
app.get('/guilds', (req, res) => {
get('https://discordapp.com/api/v6/users/@me/guilds')
.set('Authorization', `Bearer ${req.query.token}`)
.then(response => res.json(response.body))
.catch(console.error);
});
app.listen(8080, () => console.log('Ready'));
{
"name": "oauth2",
"version": "1.0.0",
"description": "Basic OAuth2 Example",
"private": true,
"main": "app.js",
"dependencies": {
"btoa": "*",
"express": "*",
"snekfetch": "*"
}
}
@Vens0n
Copy link

Vens0n commented Jul 26, 2020

Missing "redirect_uri" in request.
Is This My Error, Or Yours?

@GeniusTimo
Copy link

You have to add the redirect uri to your application through the developer portal

grafik

@acontor
Copy link

acontor commented Aug 12, 2020

I have an error. I've recived /guidls?token=undefined when /authorize redirect.

response.body.access_token = undefined...

any idea?

Copy link

ghost commented Oct 26, 2020

Mine says
error: 'invalid_request', error_description: 'Missing "code" in request.'
Any help?

@CodebotYT
Copy link

how to run it

@matrndev
Copy link

matrndev commented Jan 9, 2021

Mine says
error: 'invalid_request', error_description: 'Missing "code" in request.'
Any help?

discord/discord-api-docs#1701 (comment)

@PasinduDushan
Copy link

Missing "redirect_uri" in request.
Is This My Error, Or Yours?

You have to add your redirect URI. Add this if you're using localhost. http://localhost:8080/authorize

@ExtremeStylez
Copy link

Mine Says 400 Bad Request

@xxAROX
Copy link

xxAROX commented Dec 11, 2021

I get 400 or 401 status code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment