Created
September 20, 2020 13:08
-
-
Save dmdboi/6938a7eac81a8070270908e9581c5db1 to your computer and use it in GitHub Desktop.
Two functions to authenticate a Discord User and get User's data
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
var axios = require('axios'); | |
var qs = require('qs'); | |
exports.getAccessToken = (code) => { | |
var data = qs.stringify({ | |
'client_id': '', | |
'client_secret': '', | |
'grant_type': 'authorization_code', | |
'code': code, | |
'redirect_uri': '', | |
'scope': 'identity email' | |
}); | |
var config = { | |
method: 'post', | |
url: 'https://discord.com/api/oauth2/token', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
data : data | |
}; | |
return axios(config) | |
.then(response => { return response.data }) | |
.catch(function (error) { | |
console.log(error); | |
}); | |
} | |
exports.getUser = (access_token) => { | |
var axios = require('axios'); | |
var config = { | |
method: 'get', | |
url: 'https://discord.com/api/users/@me', | |
headers: { | |
'Authorization': `Bearer ${access_token}` | |
} | |
}; | |
return axios(config) | |
.then(response => { return response.data }) | |
.catch(function (error) { | |
console.log(error.data); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment