OAuth2: Discord API всегда отвечает {"error": "invalid_grant"} - PullRequest
1 голос
/ 17 июня 2020

Я пытаюсь реализовать Discord OAuth2 в моем node.js приложении. Как только я пытаюсь получить токен доступа из данного кода авторизации, я всегда получаю 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')
}

1 Ответ

1 голос
/ 18 июня 2020

Ладно решил проблему. Для всех, кто столкнулся с той же проблемой, что и у меня, я решил ее, используя ax ios и querystring для отправки запроса POST в Discord API (https://github.com/discord/discord-api-docs/issues/1131)

It Кажется, что есть проблема с форматом JSON и x- www-form-urlencoded.

...