'Discord Oauth2 Join Guild

const guildMembersResponse = fetch(`http://discordapp.com/api/guilds/440494010595803136/members/278628366213709824`,
            {
              method: 'PUT',
              headers: {
                Authorization: `Bearer TOKEN`,
              },
            });
            setTimeout(() => {
                console.log(guildMembersResponse)
            }, 500);

I want to join a user to my Discord server with his userid and his token in nodejs, but if I request the Dicord api I get an error:

Promise {
 Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: { body: [PassThrough], disturbed: false, error: 
  null },
  [Symbol(Response internals)]:
  { url:
  'https://discordapp.com/api/guilds/440494010595803136/members/278628366213709824',
   status: 401,
   statusText: 'UNAUTHORIZED',
   headers: [Headers] } } }

I am using the node-fetch library!



Solution 1:[1]

A 401 error would mean that you haven't provided the correct scope for the oauth link. Read more about Discord's OAuth scopes over at https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes. Quoting the docs, you would need the guilds.join scope.

Moreover, the docs quote:

guilds.join and bot require you to have a bot account linked to your application. Also, in order to add a user to a guild, your bot has to already belong to that guild.

So make sure your app fulfills the above requirement.

If you have made sure that you have followed the above steps, and you still cannot get it working, you would have to share the OAuth link with us to help you investigate further

Solution 2:[2]

You should add user token into the body, Like this:

{
  method: 'PUT',
  access_token: "Bearer TOKEN"
  headers: {
    Authorization: `Bot TOKEN`,
}

user token should be like this : TiaRZjWv5YAp80MpTFRkhi1GhXqddB

Solution 3:[3]

One thing I did to make it work was to use Discord's REST. unfortunately this documentation is very poorly prepared.

const { REST } = require('@discordjs/rest');

const rest = new REST({ version: '10' }).setToken('TOKEN');

rest.put(`/guilds/${guildID}/members/${userID}`, {
    body: {
        'access_token': accessToken
    }
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 slideshowp2
Solution 3 Zikkado