【问题标题】:400 Error when requesting a token from Discord API从 Discord API 请求令牌时出现 400 错误
【发布时间】:2021-03-22 01:41:35
【问题描述】:

我正在尝试使用 Discord 的 API 创建登录过程。根据文档,我假设使用回调 URL 上返回的 code 进行另一个调用以接收访问令牌。我已经按照here 的说明进行操作。但是,我在拨打电话时不断收到400 错误。

对我需要更正的内容有什么帮助吗?

let options = {
  method: 'POST',
  headers: {
    'Authorization': 'Basic ' + discordInfo.client_id + ":" + discordInfo.client_secret,
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data: {
    'client_id': discordInfo.client_id,
    'client_secret': discordInfo.client_secret,
    'grant_type': 'authorization_code',
    'code': req.query.code,
    'redirect_uri': discordInfo.callbackUrl,
    'scope': 'identify email'
  }
}

let discord_data = await fetch('https://discord.com/api/oauth2/token', options).then((response) => {
  if (response.status >= 400) {
    throw new Error("Bad response from server");
  }
  return response.json();
}).then((response) => {
  console.log('Discord Response', response);
}).catch((error) => {
  console.log(error);
});

【问题讨论】:

    标签: node.js discord fetch discord.js http-status-code-400


    【解决方案1】:

    经过进一步研究,我发现了以下video 帮助了我。

    这是工作代码:

    let options = {
      url: 'https://discord.com/api/oauth2/token',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: new URLSearchParams({
        'client_id': discordInfo.client_id,
        'client_secret': discordInfo.client_secret,
        'grant_type': 'client_credentials',
        'code': req.query.code,
        'redirect_uri': discordInfo.callbackUrl,
        'scope': 'identify email'
      })
    }
    
    let discord_data = await fetch('https://discord.com/api/oauth2/token', options).then((response) => {
      return response.json();
    }).then((response) => {
      return response;
    });
    

    【讨论】:

      猜你喜欢
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多