Discord api get guild

Viewed 81

I'm trying to fetch my guild info from discord API

fetch('https://discord.com/api/guilds/772037458996101140', {
            headers: {
                authorization: 'bot Nzc******zNjYzOTAz.GPwhCV.rhIcH****5R8ZS-cIo4MjPcBVxO6wYsUXhY'
            },
        })
            .then(result => result.json())
            .then(response => {
                console.log(response);
            })
            .catch(console.error)

only reciving 401 (unauthorized)

i tried changing "bot" to "BOT", "Bot", "Bearer" and doing authorization using access token from Oauth2

2 Answers

The problem lies in how the Authorization header has been specified. The correct way specify headers would be:


fetch('https://discord.com/api/guilds/772037458996101140', {
            withCredentials: true,
            credentials: 'include',
            headers: {
            'Authorization': 'bot Nzc******zNjYzOTAz.GPwhCV.rhIcH****5R8ZS-cIo4MjPcBVxO6wYsUXhY',
            },
        })
            .then(result => result.json())
            .then(response => {
                console.log(response);
            })
            .catch(console.error)

Hope this helps.

Edit

Also added crendentials and withCredentials based on this answer.

I was sending http request from front-end and when i moved it to back-end or just used python test file it worked using : Authorization: 'Bot <TOKEN>'

not sure why but it works how it should mby somebody can explain ?

Related