【问题标题】:UnhandledPromiseRejectionWarning: DiscordHTTPError: 401 Unauthorized on GET /api/v7/users/@meUnhandledPromiseRejectionWarning: DiscordHTTPError: 401 Unauthorized on GET /api/v7/users/@me
【发布时间】:2021-04-27 11:56:01
【问题描述】:

我正在使用 discord-oauth package 使用 Discord 制作登录系统...但它给出了未经授权的错误 (UnhandledPromiseRejectionWarning: DiscordHTTPError: 401 Unauthorized on GET /api/v7/users/@me)

代码:

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)
    })

【问题讨论】:

    标签: node.js oauth-2.0 callback discord


    【解决方案1】:

    我自己也有类似的问题,但我很确定它与您的原因不同。这是我注意到的错误。

    错误 1) tokenRequest 没有返回一个 refresh_token,它返回一个包含 { access_token, refresh_token, ...etc }

    错误 2) getUser 使用 access_token,但是您正尝试使用 refresh_token 进行此操作

    希望对你有所帮助^-^

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 2020-07-01
      • 2021-02-11
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多