我正在尝试在我的node.js 应用程序中实现Discord OAuth2。一旦我尝试从给定的授权代码获取访问令牌,我总是会收到 HTTP 响应错误 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')
}
Run Code Online (Sandbox Code Playgroud)