开发网站集成Discord OAuth2时遭遇unsupported_grant_type错误求助
解决Discord OAuth2的
unsupported_grant_type错误 你的问题出在axios请求的参数传递方式上:axios的POST请求中,应该用data字段来传递请求体内容,而不是body。这会导致Discord服务器无法正确解析你发送的grant_type参数,从而返回unsupported_grant_type错误。
修正后的代码如下:
function getToken(code) { const data = new URLSearchParams({ client_id: 'ops', client_secret: 'ops', code: code, grant_type: 'authorization_code', redirect_uri: 'http://localhost:5500/callback', scope: 'identify' }) axios({ url: 'https://discord.com/api/oauth2/token', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: data.toString() // 替换原有的body字段为data }).then(response => { return response.data }) }
另外再检查几个关键点:
- 确保
redirect_uri和你在Discord开发者平台中配置的完全一致(包括协议、域名、端口、路径) - 确认
client_id和client_secret是Discord应用的正确凭据 - 确保
grant_type的值确实是authorization_code,没有拼写错误
内容的提问来源于stack exchange,提问作者Acerola Gamer
相关产品推荐
相关产品推荐

