Created
October 15, 2017 19:07
-
-
Save xDimGG/38e17b78f4e070c317e603c06cecbb33 to your computer and use it in GitHub Desktop.
Basic Discord OAuth2 Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "oauth2", | |
"version": "1.0.0", | |
"description": "Basic OAuth2 Example", | |
"private": true, | |
"main": "app.js", | |
"dependencies": { | |
"btoa": "*", | |
"express": "*", | |
"snekfetch": "*" | |
} | |
} |
how to run it
Mine says
error: 'invalid_request', error_description: 'Missing "code" in request.'
Any help?
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
Mine Says 400 Bad Request
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
Mine says
error: 'invalid_request', error_description: 'Missing "code" in request.'
Any help?