You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Discord API oauth2/token接口报Grant type None is not supported如何解决

问题根源

你报错的核心原因是请求格式和声明的Content-Type不匹配:

  • 你在headers里声明了Content-Type: application/x-www-form-urlencoded,但是提交的body是JSON序列化后的字符串,Discord的OAuth接口无法正确解析表单参数,导致识别不到grant_type字段,才会返回Grant type None is not supported的错误。

修复方案

你可以用以下两种方式修改代码:

方案1:保持x-www-form-urlencoded格式,传入表单序列化后的内容

用node内置的querystring模块把参数对象转成符合表单格式的字符串,不要用JSON序列化:

// 先在文件头部引入querystring模块,无需额外安装
const querystring = require('querystring');

app.get('*', (req,res) => {
  res.sendFile(__dirname + "/test.html")
  console.log(req.query.code)
  const Body = {
      client_id: "870076989682360330",
      client_secret: "my secret",
      code: req.query.code,
      redirect_uri: "http://localhost:3000",
      grant_type: "authorization_code",
      scope :"scope"
  }
  if(req.query.code) {
      reqst.post({
          uri: "https://discord.com/api/oauth2/token",
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          // 替换为querystring序列化
          body: querystring.stringify(Body)
      }, (x,y,z) => {
          console.log(z)
      })
  }
})

方案2:修改Content-Type为JSON格式

如果要传JSON格式的body,就把headers里的Content-Type对应修改为application/json:

reqst.post({
    uri: "https://discord.com/api/oauth2/token",
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(Body)
}, (x,y,z) => {
    console.log(z)
})

注意事项

  • 生产环境不要把client_secret硬编码在可公开访问的代码里,避免密钥泄露
  • 建议在发送请求前增加参数校验逻辑,避免无效请求

内容的提问来源于stack exchange,提问作者rllynotfox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 06:15:05