1

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 receiving 401 (unauthorized)

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

0

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.

Aastha Bist
  • 324
  • 2
  • 11
  • Your answer is wrong, because the second argument of `fetch` is for options, not headers. – Konrad Jul 24 '22 at 18:28
  • thank you for responding, but nothing changed... i forgot to mention that i use the same authorization method for another discord API requests (with "beaver" not "bot" authorization) and it works fine – Rozbitá Žaluzie Jul 29 '22 at 11:38
0

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 ?

0

Not sure why you were having getting the 401 response but I do see one issue, the url you are sending a request to doesn't specify what API version to use.

The url would be https://discord.com/api/v9/guilds/772037458996101140

And maybe consider using the discord api exclusively on the backend if you are making your site public, because it leaves your token in your source code for anyone to yoink

Code Disease
  • 64
  • 1
  • 3
  • 10