Discord OAUTH2 Add an user to a guild

Viewed 82

I am trying to make a put request to the Discord API, in order to add the user to a guild. Here is my code :

app.get("/api/auth/joinguild", async (req: Request, res: Response) => {
    try {
        const body = new url.URLSearchParams({
            "access_token": `${accessToken}`
        })
        const guild_id = "HIDDEN";
        const user_id = "HIDDEN";
        const botToken = "HIDDEN"
        const response = await axios.put(`https://discord.com/api/v10/guilds/${guild_id}/members/${user_id}`,
        body.toString(),
            {
                headers: {
                    Authorization: `Bot ${botToken}`,
                    'Content-Type': 'application/json',
                },
            });
        res.send(await response.data)
    } catch (err) {
        res.send(await err);
    }
});

The accessToken I have is correct since I can use it to call endpoints such as https://discord.com/api/v10/users/@me

1 Answers

I managed to solve this problem with the following request :

        const response = await axios({
        method: 'put',
        url: `https://discord.com/api/v10/guilds/${guild_id}/members/${user_id}`,
        data: {
            "access_token": accessToken,
        },
        headers: {
            Authorization: `Bot ${botToken}`,
            'Content-Type': 'application/json',
        },
    });

The reason why it wasn't working is because the PUT request wasn't correctly formatted. The 'data' must be JSON formatted it was encoded as an url.

Related