gpt4 book ai didi

javascript - OAuth2 : Discord API always responds with {"error": "invalid_grant"}

转载 作者:行者123 更新时间:2023-12-03 07:08:09 28 4
gpt4 key购买 nike

我正在尝试在我的 node.js 应用程序中实现 Discord OAuth2。一旦我尝试从给定的授权代码中获取访问 token ,我总是会收到 HTTP 响应 Error 400 {"error": "invalid_grant"}

let xhr = new XMLHttpRequest()
xhr.open('POST', 'https://discord.com/api/oauth2/token')

xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

let payload ={
client_id: clientID,
client_secret: clientSecret,
grant_type: 'authorization_code',
code: code,
redirect_uri: redirectUrl,
scope: 'identify'
};

console.log(payload)
xhr.send(JSON.stringify(payload))

xhr.onreadystatechange = () => {
console.log(xhr.status)
console.log(xhr.responseText)
}

xhr.onerror = () => {
console.log('Failed')
}

最佳答案

好的,我解决了这个问题。对于遇到与我相同的问题的每个人,我通过使用 axios 和 querystring 将 POST 请求发送到 Discord API ( https://github.com/discord/discord-api-docs/issues/1131 ) 来解决它

JSON 和 x-www-form-urlencoded 格式似乎有问题。

关于javascript - OAuth2 : Discord API always responds with {"error": "invalid_grant"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62433923/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com