【问题标题】:Clash Royale Node Fetch with Key使用密钥获取 Clash Royale 节点
【发布时间】: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


【解决方案1】:

由于皇室战争 API 使用 bearer authentication,您需要指定它将是不记名令牌。

headers: {
  'Authorization': `Bearer ${token}`
}

【讨论】:

    【解决方案2】:

    我已经实现了以下功能。代码是用 GO 编写的,但您可以复制逻辑并翻译成您的语言。

    该库具有以下功能:

    • 登录
    • 令牌生成
    • 令牌列表
    • 令牌删除

    https://github.com/alessiosavi/GoClashRoyale/blob/master/api/auth.go

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      相关资源
      最近更新 更多