I'm making a login system with Discord using the discord-oauth package ... But it gives this error of unauthorized
(UnhandledPromiseRejectionWarning: DiscordHTTPError: 401 Unauthorized on GET /api/v7/users/@me)
Code:
const router = require('express').Router()
const crypto = require('crypto')
const DiscordOauth2 = require('discord-oauth2')
const request = require('snekfetch')
const rateLimit = require('../middleware/limiterBotControler.js')
const baseUrl = 'https://discord.com/api/v7/'
const oauth = new DiscordOauth2({
clientId: '782761260620972034',
clientSecret: '',
redirectUri: 'http://localhost:8080/login/discord/callback'
})
router.get('/login', (req, res) => {
let url = oauth.generateAuthUrl({
scope: ['identify', 'email', 'guilds'],
state: crypto.randomBytes(16).toString('hex')
})
return res.redirect(url)
})
router.get('/login/discord/callback', (req, res) => {
let code = req.query.code
oauth.tokenRequest({
code: code,
grantType: 'authorization_code',
scope: ['identify', 'email', 'guilds']
}).then((refresh_token) => {
oauth.getUser(refresh_token).then(console.log)
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…