我正在嘗試向 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);我試過使用硬編碼值在郵遞員中執行請求,并且得到相同的響應,因此我認為這不是語法錯誤,但我無法確定。在此先感謝您的幫助。
Discord API 使用機器人令牌為所有查詢返回 401
冉冉說
2021-12-02 19:12:13