【问题标题】:Axios GET request to the discord API gives a 403 forbidden response, but works in Postman对 discord API 的 Axios GET 请求给出了 403 禁止响应,但在 Postman 中有效
【发布时间】:2020-08-20 06:50:25
【问题描述】:

我正在使用 axios 向本地环境中的 discords API 发出 HTTP GET 请求。

我正在使用以下代码:

axios.get('https://discord.com/api/channels/735303230694621228', {
    withCredentials: true,
    headers: {
      Authorization: 'Bot ' + botToken,
    },
  }).then((data) => {
    res = data.response
  })

Bot 是频道 ID:735303230694621228 的所有者,并且拥有所有正确的权限。

实际结果:

预检请求 (OPTIONS) 给出的响应为 200,并带有所有正确的标头。

预检后,GET 请求将发生并给我以下响应:

403 Forbidden

浏览器也会给出 CORS 错误,但我很确定这是因为 403 响应,因为 403 没有给出所有正确的 CORS 标头。

预期结果:

Status 200 OK

我尝试https://discord.com/api/channels/735303230694621228 与 Postman 使用相同的 Bot 令牌,它给了我一个 200 响应和预期的正文。

有没有人知道为什么我在本地环境中通过浏览器使用 axios 时收到 403,但我收到邮递员的 200 响应?

【问题讨论】:

  • 似乎 chatID 变量实际上并没有被替换。
  • 假设您有一个名为 channellD 的变量,对字符串使用记号 ` 而不是括号 "

标签: javascript rest axios postman discord


【解决方案1】:

Discord 在 some API 资源上以 403 响应 some User-Agents。

要对此进行测试,请发送一个 Postman 请求,并将您的 User-Agent 标头设置为与浏览器匹配的值,例如 User-Agent: Mozilla/5.0

遗憾的是,浏览器将覆盖 axios.get() 中的 User-Agent 标头,即使您在 .get() 的配置中定义了一个。

以上是 Postman (User-Agent: PostmanRuntime/7.0.0) 收到 200 响应,而您的浏览器 (User-Agent: Mozilla/5.0) 收到 403 的原因。


值得注意的是,Discord API 文档没有解释这一点(截至 2020 年 8 月)https://discord.com/developers/docs/resources/channel#get-channel

【讨论】:

  • 谢谢!这是我的问题。最后,我使用代理将用户代理标头更改为 (User-Agent: Mozilla/5.0) 以外的其他内容
  • @Charles 您是如何更改用户代理的...?我很stuuuuck
  • @Aastal 您可以将axios.get() 指向 Nginx 服务器的 URL,其 .conf 类似于... server { listen 80; server_name localhost; location / { proxy_pass https://discord.com; proxy_set_header User-Agent 'Not-a-browser...honest'; } } 或者对另一个 Web 服务器使用类似的技术(可能是服务器默认配置或虚拟主机配置)。
猜你喜欢
  • 1970-01-01
  • 2016-08-17
  • 2017-11-28
  • 1970-01-01
  • 2022-01-18
  • 2020-09-27
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
相关资源
最近更新 更多