Skip to content

Instantly share code, notes, and snippets.

@dmdboi
Created September 20, 2020 13:08
Show Gist options
  • Save dmdboi/6938a7eac81a8070270908e9581c5db1 to your computer and use it in GitHub Desktop.
Save dmdboi/6938a7eac81a8070270908e9581c5db1 to your computer and use it in GitHub Desktop.
Two functions to authenticate a Discord User and get User's data
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