Search code examples
node.jsexpressoauth-2.0discordnode-fetch

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

  • I just had to replace the "ContentType" to "Content-Type" in the headers!

    headers: {
      "Authorization": "Bot [botToken]",
      "Content-Type": "application/json",
    }