#Help Using the Discord API

304 messages · Page 1 of 1 (latest)

glossy flume
#

I am attempting to add a guild member but I'm getting a 401 (unauthorized) repsponse, please help.

#
                var guildResponse = await fetch(`https://discord.com/api/guilds/${guildId}/members/${userId}`, {
                method:"PUT",
                    headers: {
                        authorization: `{BOT TOKEN HERE}`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        access_token:authToken
                    })
                })

                guildResponse = await guildResponse.json()

                console.log(guildResponse)
alpine briar
#

You can't add members

glossy flume
#

oauth2

#

I've got the oauth2 token with the guild.add scope

alpine briar
#

What do you mean add?

glossy flume
light mountain
#

Do you have the users Oauth2 token to do that?

glossy flume
#

yep

#

and I'm sure it works as ive tested it on another request

alpine briar
#

Oh weird, that must be a new API endpoint

light mountain
#

Yeah, it is a new endpoint

alpine briar
#

Are you getting an error response?

glossy flume
light mountain
#

But having the bot have the Oauth2 scope for that is not enough, you need the users token

glossy flume
#

users token is authToken

#

from what I understood the endpoint wants it in the body

#

unsure about it tho

#

users token is what you get from using the oauth2 link right?

#

just so I understand you

light mountain
#

You mean the invite link for the bot?

alpine briar
#

The user should have been sent to the authorization web page which then gives you an auth token

glossy flume
#

no the like

glossy flume
#

I did that

#

and thats the token im talking about

alpine briar
#

Ok, does your bot have invite perms on the server?

glossy flume
#

yep, full admin perms

alpine briar
#

You can't just set the token. You've gotta indicate the token type.

authorization: `BOT {BOT TOKEN HERE}`,
glossy flume
#

oh

alpine briar
#

Here's the docs

glossy flume
#

I think that's correct

#

but I'm still getting a 401

#

actually

#

wait 1 sec

#

getting a 403 now 😐

light mountain
#

And you have that scope set under the bot scopes in the dev portal?

glossy flume
#

yep administrator

alpine briar
#

And the oauth page is actually authorizing a guild join?

glossy flume
#

the scopes i selected for oauth were identify and guild.join

alpine briar
#

You can't just change the oauth perms. You've gotta get a new token if you do.

glossy flume
#

I had them like that since the beginning

#

new token:

#

actually small question

#

the oauth token can be used several times

#

as long as it hasn't expired

#

correct?

#

since if not then that's the cause

alpine briar
#

It should be

glossy flume
#

yeah it is

alpine briar
#

You're not getting json back, you're getting HTML. Try dumping that HTML and seeing what it says

glossy flume
#

just added an identify

wary moat
#

@glossy flume make a bot

glossy flume
#

;-;

wary moat
alpine briar
#

They have a bot working ...

wary moat
#

oh my bad

glossy flume
#

hold on @alpine briar does the bot and bearer thing apply to the access_token param as well?

alpine briar
#

They're trying to get the endpoint to authorize and we're in the process of debugging that

glossy flume
#

shit ok

wary moat
#
// Get the user's access token
const response = await fetch(`https://discord.com/api/oauth2/token`, {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  body: `client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=YOUR_REDIRECT_URI`
});

const { access_token } = await response.json();

// Add the user to the guild
const guildResponse = await fetch(`https://discord.com/api/guilds/${guildId}/members/${userId}`, {
  method: "PUT",
  headers: {
    authorization: `Bot ${BOT_TOKEN}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    access_token
  })
});

const guildData = await guildResponse.json();

console.log(guildData);
#

oh the authorize doesnt work?

glossy flume
#

accessToken is authToken

alpine briar
#

@wary moat you're not helping rn

#

We're already debugging what's wrong

glossy flume
#

I just did access_token:authToken

alpine briar
#

Did you do that change to that code?

glossy flume
#

yeah

#

btw when debugging with insomnia

#

it works

#

so its probably something relating to the code

alpine briar
#

Were you able to dump the HTML to see what's wrong? I think cloudflare may be blocking you

glossy flume
#

Still trying

#

but this is the new code

#

shit forgot to blur token

#

1 sec

alpine briar
#

Lol

glossy flume
#
var guildResponse = await fetch(`https://discord.com/api/guilds/810512343175528539/members/${userId}`, {
                method:"PUT",
                    headers: {
                        authorization: `Bot TOKEN`,
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        access_token:authToken
                    })
                })
alpine briar
#

So you have the word "Bot" followed by a space and then the token?

glossy flume
#

thats what it was in identify as well so i think it should be like that yea

alpine briar
#

Ok that's correct

glossy flume
#
var userResponse = await fetch('https://discord.com/api/users/@me', {
                    headers: {
                        authorization: `${authType} ${authToken}`,
                    },
                })
#

identify btw incase you need it

alpine briar
#

Ok so that's giving the 403, are you able to log out the HTML it's giving you

wary moat
#
const guildResponse = await fetch(`https://discord.com/api/guilds/810512343175528539/members/${userId}`, {
  method: "PUT",
  headers: {
    authorization: `Bot ${BOT_TOKEN}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    access_token: authToken
  })
});

console.log(guildResponse);
``` log the guild respond
glossy flume
#

im gonna be honest with you im trying but im unable to

alpine briar
#

Just wanna determine if it's cloudflare before I try to explain how to fix it

glossy flume
#

1 sec let me try something else ig

#

I can't replicate the error on insomnia

#

hm

wary moat
#

You can try using a tool like Postman or curl to make a request to the API endpoint manually to see if you get the same error. This can help you determine if the issue is with your code or if there is a problem with the API itself.

alpine briar
#

@wary moat please let us debug THE CODE

wary moat
#

ok

glossy flume
#

body returns a readableStream

#

hmmm

alpine briar
#

just console log the guild response variable

glossy flume
#

yeah it doesn't give the html

#

let me try logging text

#

got it

wary moat
#
var guildResponse = await fetch(`https://discord.com/api/guilds/810512343175528539/members/${userId}`, {
    method:"PUT",
    headers: {
        authorization: `Bot TOKEN`,
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        access_token:authToken
    })
});

guildResponse = await guildResponse.json();

console.log(guildResponse);  // Log the entire response object
console.log(guildResponse.status);  // Log the HTTP status code
console.log(guildResponse.message);  // Log any error message included in the response
``` gotta go into it again
#

i read that wrong thoguht u said http

glossy flume
#

let me open an html file with the data i got

#

@alpine briar its cloudflare

alpine briar
#

Ok you need to set a user agent

glossy flume
alpine briar
#

Lemme get the docs

glossy flume
#

cant show details as dont wanna get hacked

alpine briar
#

That'll go on the header section

glossy flume
#

just to understand

#

have I been rate limited

#

or what happened

alpine briar
#

No

#

They just want you to have a user agent

glossy flume
#

ok

alpine briar
#

Any luck?

glossy flume
#

trying to understand the user-agent stuff

#

but this doesnt work "User-Agent":"groupname/1.0"

#

oh wait discord has a format

alpine briar
#

Yeah follow their format

#

It should work

glossy flume
#

what url and version number is it refering to exactly

alpine briar
#

Anything you want to put.

glossy flume
#

is it optional

#

or

alpine briar
#

The idea is it would be the website for your bot and your bots version

glossy flume
#

guessing localhost is not a website

#

havnt yet got a domain

alpine briar
#

I don't think it checks the validity

glossy flume
#

still 403

#

sigh

#

"User-Agent":"DiscordBot (https://discord.com/, 1.0)"

alpine briar
#

Strange

#

I used my GitHub repo URL

glossy flume
#

or im extremely stupid

#

nope google doesn't work either

#

hm

alpine briar
#

What's the updated code

glossy flume
#

403

#

if youre talking about teh html let me check

alpine briar
#

Nah the code you wrote

glossy flume
#
var guildResponse = await fetch(`https://discord.com/api/guilds/810512343175528539/members/${userId}`, {
                method:"PUT",
                    headers: {
                        authorization: `Bot TOKEN HERE`,
                        "Content-Type": "application/json",
                        "User-Agent":"DiscordBot https://google.com/, 1.0"
                    },
                    body: JSON.stringify({
                        access_token:authToken
                    })
                })
#

removed the brackets

#

thought they may be the cause

#

but it doesn't work with or without

alpine briar
#

DiscordBot (https://google.com, 1.0)
Doesn't work?

glossy flume
#

yep

alpine briar
#

And it's still giving you a cloud flare page?

glossy flume
#

yep

alpine briar
#

Huh

#

What's odd is cloudflare doesn't check the body so it's only the headers that matter rn. So something about that must be incorrect. Are you running the code from a server or your personal computer?

glossy flume
#

localhost right now

#

using a server to host the pages

#

server = (express.js)

alpine briar
#

And the server is able to go through all the oauth steps without issue?

glossy flume
#

yep it works with the identify request

#

so it isnt my ip adress

#

its probably the header a wrong letter or something

#

but ive checked 5 times

#

nothing seems to be off

alpine briar
#

You're missing the API version number

#

In the endpoint url

glossy flume
#

discord api?

alpine briar
#

https://discord.com/api/v{version_number}

glossy flume
#

nah that gotta be fake

alpine briar
glossy flume
#

doesnt yet works

#

im gonna lose it if its a url error

alpine briar
#

It is lol

#

I hope anyways

glossy flume
#

403

#

thank god

#

used v10

alpine briar
#

OMG haha

glossy flume
#
var guildResponse = await fetch(`https://discord.com/api/v10/guilds/810512343175528539/members/${userId}`, {
                method:"PUT",
                    headers: {
                        authorization: `Bot TOKEN`,
                        "Content-Type": "application/json",
                        "User-Agent":"DiscordBot (https://google.com/, 1.0)"
                    },
                    body: JSON.stringify({
                        access_token:authToken
                    })
                })
alpine briar
#

I'd leave that. Without it you're defaulted to the deprecated v6 API

glossy flume
#

oh-

#

who made it default to v6 tho-

#

and more importantly, why

alpine briar
#

Probably for legacy reasons

glossy flume
#

idk at this point

alpine briar
#

Insomnia is running from the same computer as your code right?

glossy flume
#

yeah

#

thats whats confusing me

alpine briar
#

Can you see what the requests it makes look like?

glossy flume
#

regular request

#

nothing special

#

same exact headers i believe

#

oh hold on

#

in the time line it does define a user agent

#

but without the discord format

#

yet it works

#

yet this exact user agent doesnt work in code

alpine briar
#

Try fetching info about the bot user with a get request to /users/@me

#

Let's see if it'll let you make any requests at all

glossy flume
#

how do i oauth the bot exactly-

alpine briar
#

Not sure what you mean?

glossy flume
#

to identify i need an accesstoken to the user im accessing

#

or does the bot token also work

alpine briar
#

Again, not sure what you mean

glossy flume
#

it worked

#

i got a regular response

#

no errors

alpine briar
#

For the users/@me?

glossy flume
#

yeah

#

with the bot info

alpine briar
#

Ok, so the headers seem fine...

alpine briar
alpine briar
#

You exchange the code you're given for the access token correct?

glossy flume
#

yeah i get the code from the oauth page, then use it to get the access token and token type and expiry, etc..

glossy flume
#

may it be the endpoint unoperational

#

or requires a verified app with over 100+ servers

alpine briar
#

And you're definitely requesting the guild join permission in the oauth

glossy flume
#

scope=identify%20guilds.join

#

directly from the oauth link

#

brb

alpine briar
#

Can you link the docs for the join guild endpoint

light mountain
#

I feel like the data gotta be filled entirely for that to even attempt to go through

#

access_token, nick, roles, mute, deaf

alpine briar
#

It says everything but access token is optional. It'll default to the username, no roles, false, and false. Same as a user following an invite link.

light mountain
#

Ahh

glossy flume
#

back

#

any news?

#

doesnt seem like it

#

ok

alpine briar
#

Ok

  • make sure the bot token belongs to the same app as the oauth authorization
  • make sure the guild id is correct
  • make sure the user id is correct

Beyond that I dunno what to tell you. Everything looks correct.

glossy flume
#

insomnia worked

#

so all that must be correct

#

else it wouldnt have

#

should i contact discord support or?

#

may be a problem with their cloudflare

#

actually ive got an idea but ill need to do it tmrw as i gotta head off for now

#

ill use another endpoint

#

if we can confirm that the other endpoint works with the same headers

#

then its something relating to the endpoint for guild.join

#

in which ig we can confirm its not from our side

#

but I dont believe discord would leave something like that unnoticed

#

hm

glossy flume
#

anyways contiue tmrw ig, thanks for the help till now tho

#

as ik that this is taking up a lot of your time

alpine briar
#

Well you were able to use the /users/@me API endpoint, so the headers are fine it seems. Are you on the official Discord Developers server? They have an API questions channel.

alpine briar
# glossy flume ill use another endpoint

I'd suggest using the /users/@me endpoint again but as a PATCH request. It'll let you send a body payload, so it'll be very similar to the join endpoint. Just give your bot a new name
https://discord.com/developers/docs/resources/user#modify-current-user

That or use the /users/@me/channels with a POST request to create a DM channel
https://discord.com/developers/docs/resources/user#create-dm

Discord Developer Portal

Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.

#

The /users/@me endpoints are good for debugging API issues because they don't require special permissions.

glossy flume
#

Sounds good will try tmrw 👍

#

Thanks again

alpine briar
#

Np

glossy flume
#

as the token im sending is the bot's token

#

not the access token of oauth2

glossy flume
#

since you're not passing anything to the headers this time

#

but ill try it regardless

#

hold up what

#

I'm getting a 401 for the patch to /users/@me

#

nvm its working now

#

turns out it did need the bot token for authorization

#

guess I may have missed it in the docs

#

even without user-agent

#

guessing the error must be with the endpoint for adding user guilds then..?

alpine briar
glossy flume
#

still trying to find a solution tho

alpine briar
#

Maybe I'll try and see if I can't get the API to work for me tonight.

glossy flume
#

api server no response

glossy flume
#

problem is this is the last feature remaining for my application

#

gonna hope a solution is found soon

#

since I cant work on anything till it works

alpine briar
#

I have a working discord API wrapper written in Python. So I know I have everything working. I'll just need to use the endpoint and see if it works for me.

glossy flume
#

alright

#

if in dire cases we cant get it to work I believe I can probably make it to be manually done by the admin for now

#

although that will be an inconvenience but I believe should be good enough till its fixed

alpine briar
#

Ended up not having time tonight. I'm gonna leave this post unread and look into it tomorrow evening.

alpine briar
#

Just wanna bump this. Holidays are really making it hard to get stuff done.

glossy flume
#

alright

#

I'll contact discord support regardless

#

the fact that the api server no one responded

#

gives me a little bit of suspicion that it may be an api side error

glossy flume
#

@alpine briar any updates?

alpine briar
#

Sorry. I've been very busy with the holiday season.