V
V
vahelce2021-06-26 21:52:08
Node.js
vahelce, 2021-06-26 21:52:08

How to make authorization on the site through OAauth2 Discord?

Good afternoon, the second day trying to resolve the issue.
I need to authorize the site through Discord. I created an application, generated a special link to get a token (such as this: https://discord.com/api/oauth2/authorize?response_... ), log in, it redirects me to the site, but authorization through Discord does not work - the API returns 401 Not Authorized. I use axios for http requests.

According to the documentation, for authorization, you must pass the token in the header when accessing the API.

async function login({ access_token }) {
    if (!access_token || access_token === "") {
        return {
            success: false,
            reason: "The token is not specified"
        };
    }
    /**
     * Вернет информацию о пользователе если всё в порядке
     */
    await require('axios').get('https://discord.com/api/v9/users/@me', {
        Headers: {
            Authorization: 'Bearer ' + access_token
        }
    }).then(r => {
        console.log(r.data);
        return {
            success: true
        };
    }).catch(e => {
        if (e.response) {
            console.log(e.response.status); // 401 всегда!
        } else {
            console.log(e.message);
        }
    });
}
export { login };

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question