“客户端无效错误”OAuth2 的 Discord API

问题描述 投票:0回答:1

我正在使用 axios 和express 在 JavaScript 中编写一个基本程序,以了解有关 OAuth2 如何工作的更多信息,这时我的客户突然给出了错误。

错误:400(错误请求)

这是代码中出错的部分:

//^^^^^^^^^^
// defining web server

const formData = new url.URLSearchParams({
    client_id: webid,
    client_secret: websecret,
    grant_type: 'authorization_code',
    code: code.toString(),
    redirect_uri: redirect_uri,
});

const oauthInfo = await axios.post('https://discord.com/api/v10/oauth2/token',
    formData, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
});

// below is starting everything           

完整代码:https://pastebin.com/FbHVkKRi

javascript axios oauth-2.0 discord discord.js
1个回答
0
投票

formData 必须是 String 类型,而不是 Object。

const oauthInfo = await axios({
  method: 'POST',
  url: 'https://discordapp.com/api/oauth2/token',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data: new URLSearchParams({
    client_id: webid,
    client_secret: websecret,
    grant_type: 'authorization_code',
    code: code,
    redirect_uri: redirect_uri,
  }).toString();
});
© www.soinside.com 2019 - 2024. All rights reserved.