为了账号安全,请及时绑定邮箱和手机立即绑定

Discord API 使用机器人令牌为所有查询返回 401

Discord API 使用机器人令牌为所有查询返回 401

冉冉说 2021-12-02 19:12:13
我正在尝试向 Discord Web API 发送请求,但一直收到 401 响应代码。我可以在网上找到的几乎所有答案都来自使用不记名令牌而不是机器人令牌的人,并且更改为机器人令牌有效。我正在使用 bot 令牌,但仍然收到 401。但是,我知道此 bot 令牌是有效的,因为尝试node bot.js使用无效令牌启动会引发错误并且不会启动 bot。我现在的代码很简单const Discord = require('discord.js');const client = new Discord.Client();const auth = require('./auth.json');const axios = require('axios');const headers = {    'Authorization': `Bot ${auth.token}`};client.on('ready', () => {    console.log(`Logged in as ${client.user.tag}!`);});client.on('message', msg => {    /* If the author is a bot, do nothing */    if (msg.author.bot) {        return;    }    /* Only perform an action if the first character is ? */    if (msg.content.substring(0, 1) == '?' && msg.content.length > 1) {        var message = msg.content.substring(1).toLowerCase();        //console.log(message);        //console.log(msg);        //console.log(msg.channel.name);        switch (message) {            case 'gos':                axios.get(`https://discordapp.com/api/channels/${msg.channel.id}/messages`, headers)                .then(response => {                    console.log(response);                }).catch(err => {                    console.log(err);                });                break;            case 'dolphin':                msg.reply('dolphin', {files: [                    "https://www.dolphinproject.com/wp-content/uploads/2019/07/Maya-870x580.jpg"                ]});                break;        }    }});client.login(auth.token);我试过使用硬编码值在邮递员中执行请求,并且得到相同的响应,因此我认为这不是语法错误,但我无法确定。在此先感谢您的帮助。
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

正如我从您的问题中了解到的,您从邮递员那里得到了相同的回复(401 未经授权),因此唯一的原因是访问令牌无效或您无权对 API 或通道进行此类调用不和谐。

您应该看到的另一件事是您在 axios 中发送标头的方式,在这里我可以与您分享发送标头的正确方法: 如何在 axios 中设置标头和选项?

还要检查“auth.json”在您调用它时是否具有正确的令牌(auth.token)。


查看完整回答
反对 回复 2021-12-02
  • 1 回答
  • 0 关注
  • 470 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信