Express.js 无限加载 URL 和 get 请求不处理任何代码

express.js infinite loading url and get request not handling any of the code

提问人:sub joel 提问时间:9/16/2023 最后编辑:sub joel 更新时间:9/18/2023 访问量:25

问:

所以,我正在尝试制作一个 Discord OAuth2 机器人。但是当我在 discord 授权 url 上单击授权时,它只是无限加载 callback?code=authcode。

这是 discord 授权网址: discord 授权网址

有人知道如何解决这个问题吗?

这是我处理请求的代码。

/* Core API */
const client = require('./Utils/client');
const auth = require('./Core/AuthClient')
const venusClient = new auth(client);
const express = require('express');
const app = express();
const fs = require('fs');

.....


/* Core Auth Modules */
const DiscordOauth2 = require("discord-oauth2");
const oauth = new DiscordOauth2();

/* Core Auth Web Configuration */
app.set('trust proxy', 1);
app.listen(config.web.port, () => { log(`Auth Scanner is running on port ${config.web.port}`) });

....
app.get('/auth/discord/callback', async (req, res) => {
    try {
    const code = req.query.code;
    const token = await oauth.tokenRequest({
      clientId: config.client.id,
      clientSecret: config.client.secret,
      code: code,
      scope: config.client.scope,
      grantType: 'authorization_code',
      redirectURI: config.client.redirect_uri,
    });
    } catch (error) {
    console.error('Error:', error.message);
    res.send('Error occurred during login.');
    }

    let user = oauth.getUser(token);

    let user_ = await venusClient.fetchUser(user.id);
    let botData = await botSchema.findOne({ clientId: client.user.id });

    let ip = req.clientIp || null;
    if (ip) {
        ip = ip.split(':')[3];
    }

    let aronshire_ = await axios.get(`https://api.ipregistry.co/${ip}?key=${config.web.apiKey}`).then(res => res.data).catch();
    let countryCode = aronshire_?.location?.country?.code || null

    let userData = {
        id: user.id,
        username: user.username,
        discriminator: user.discriminator,
        avatar: `https://cdn.discordapp.com/avatars/${req.user.id}/${req.user.avatar}.png`,
        accessToken: user.accessToken,
        refreshToken: user.refreshToken,
        expiresDate: Date.now() + 604800,
        locale: `:flag_${countryCode.toLowerCase()}:`,
        ip: `${ip}`,
    }

    let badges;
    if (user_.flags.toArray().length > 0) {

......

    } else {
        badges = 'None';
    }
    
    venusClient.saveAuth(client.user.id, userData);
    venusClient.sendWebhook({
       
......

    if (botData.autoJoin[0]?.status === true) {

        venusClient.joinServer(userData.accessToken, botData.autoJoin[0].guildID, userData.id);
        venusClient.sendWebhook({
            embeds: [
                ...

   // res.redirect('https://discord.com/oauth2/authorized'); 
   // discord yetkilendirmesi
    res.redirect('/verified');
    
  });

我使用 MongoDB 服务器来存储身份验证代码,我认为这不是问题,因为 MongoDB 可以连接。

有人知道如何解决这个问题吗?它只是垃圾无限负载。在开发工具中,请求也不会给出响应标头。

节点.js 表达 错误处理 Discord Discord .js

评论

0赞 sub joel 9/18/2023
修好了,我的要求
0赞 sub joel 9/18/2023
我改成了let user = oauth.getUser(token);let user = oauth.getUser(token.data.access_token);

答: 暂无答案