Javascript 对于具有bot令牌的所有查询,Discord API返回401
我正在尝试向discord web API发送请求,但不断收到401响应代码。我能在网上找到的几乎所有答案都来自于那些使用承载令牌而不是bot令牌的人,并且改成bot令牌是有效的。我用的是机器人代币,还拿到401。但是,我知道此bot令牌是有效的,因为尝试使用无效令牌启动Javascript 对于具有bot令牌的所有查询,Discord API返回401,javascript,node.js,authorization,axios,discord,Javascript,Node.js,Authorization,Axios,Discord,我正在尝试向discord web API发送请求,但不断收到401响应代码。我能在网上找到的几乎所有答案都来自于那些使用承载令牌而不是bot令牌的人,并且改成bot令牌是有效的。我用的是机器人代币,还拿到401。但是,我知道此bot令牌是有效的,因为尝试使用无效令牌启动节点bot.js会抛出错误,并且不会启动bot。我现在的代码很简单 const Discord = require('discord.js'); const client = new Discord.Client(); cons
节点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);
我试着用硬编码的值在postman中执行请求,得到了相同的响应,所以我不认为这是语法错误,但我不能确定。提前感谢您的帮助。正如我从您的问题中了解到的,您从邮递员那里得到了相同的响应(401未经授权),因此唯一的原因是访问令牌无效,或者您没有权限从discord调用API或频道 您应该看到的另一件事是在axios中发送头的方式,在这里我可以与您分享发送头的正确方式:
还要检查“auth.json”在您调用时是否正确地拥有令牌(auth.token) 正如我从您的问题中了解到的,您从postman(401 Unauthorized)那里得到了相同的响应,因此唯一的原因是访问令牌无效,或者您没有权限从discord调用API或通道 您应该看到的另一件事是在axios中发送头的方式,在这里我可以与您分享发送头的正确方式: 还要检查“auth.json”在您调用时是否正确地拥有令牌(auth.token)