【发布时间】:2022-01-24 02:04:00
【问题描述】:
我正在尝试制作一个不和谐的机器人,如果你在聊天中输入 -cr,它会接受用户的参数(作为 Clash Royale 玩家的玩家标签),然后使用包 node-fetch 来接收我的数据指定的端点。我经常遇到{ reason: 'accessDenied', message: 'Invalid authorization' } 的错误。我对这些东西相当陌生,尤其是 API,但我希望访问某些我以后可以决定的数据(我知道该怎么做)。我的代码是:
const fetch = require('node-fetch')
module.exports = {
name: 'clash',
aliases: ['cr', 'clashroyale'],
category: 'This',
utilisation: '{prefix}clash',
async execute(client, message) {
var msgArgs = message.content.slice(this.name.length + 1)
var endpoint = `/players/${msgArgs}`
var url = `https://api.clashroyale.com/v1`
var token = `hidingmytoken`
fetch(url + endpoint, {
method: 'POST',
headers: {
"Authorization": token
}
}).then(data => data.json()).then(json => {
console.log(json)
})
},
};
带有 msgArgs 和 discord 方面的消息部分都可以工作,但获取冲突 Royale API 对我来说是一个很大的障碍。 Clash Royale 的 API 可以在这里找到https://developer.clashroyale.com/#/documentation,而我通常只是停留在整个概念上。我使用的是 node-fetch 的 2.6.6 版本,所以我可以使用 require() 方法,如果这很重要,它应该可以工作。一般来说,我怎样才能正确传递我的令牌来接收 API 数据?
【问题讨论】:
-
您是否指定它是不记名令牌?
-
我该怎么做?
-
“授权”:`Bearer ${token}`
标签: javascript node.js discord.js http-headers node-fetch