How to get user id from oauth on discord

Viewed 8639

Hello so i set up a discord application and OAUTH Login. I need to get user id but whatever I try there is something wrong. I readed this but it does not make any sense for me. How it knows what user it is. It says "401: Unauthorized" What am i supposed to do to authorise. also i got access token; refresh token Thanks

1 Answers

I had the same problem and just figured out the solution!

Once you successfully receive the access token, you will need to perform an http request to the following endpoint:

https://discordapp.com/api/users/@me

with the headers set to:

Authorization: Bearer ${ACCESS_TOKEN}

You should then receive the following response:

data: {
    avatar: ....,
    discrimnator: ....,
    email: ....,
    flags: ....,
    id: ....
}

You'll also get some other data points. But those are the big ones. Note: You will only receive the email field if you included that in scope when you received your access token. You can use the id received from this call to hit the users/:userId endpoint referenced in Discord's documentation.

Here is my actual code in case you're interested:

fetchUser: (data) =>
      axios
        .get(
          `https://discordapp.com/api/users/@me`,
          {headers: {Authorization: `Bearer ${data.access_token}`}}
        )

Hope that helps!

Related