I am new to Discord's API, and I am working a project which needs to be able to add a guild member programmatically. I've learned how to get an authorization code (with identify and guilds.join scopes), redeem it for an access token, and get a user's ID. The last step is to use the access code and user ID to add the guild. This command is detailed here:
https://discordapp.com/developers/docs/resources/guild#add-guild-member
It seems I need to send a PUT request to this URL:
https://discordapp.com/api/guilds/[GuildID]/members/[UserID]
But this results in this response:
{"code": 0, "message": "401: Unauthorized"}
I have tried including the access token in the Authorization header:
Authorization: Bearer [Redacted]
I've also tried adding a JSON body to the request:
{"access_token":"[Redacted]"}
Neither has worked. Unsurprisingly, using both at the same time hasn't worked either.
I wondered if this was a permissions issue, but Discord confirms I have the guilds.join scope. This is the JSON I receive when exchanging my authorization code for an access token:
{"access_token": "[Redacted]", "token_type": "Bearer", "expires_in": 604800, "refresh_token": "[Redacted]", "scope": "identify guilds.join"}
The identify scope works since I am able to retrieve the user and its ID. But guilds.join doesn't seem to work.
I have included some test code below. I have marked "Option 1" and "Option 2" lines to signify that I wouldn't typically do both of these access code methods in the same request. But as I mentioned earlier, I did try both, and I still got a 401 error.
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
client.Headers.Add(HttpRequestHeader.Authorization, "Bearer [Redacted]");//Option 1
string output = client.UploadString
(
"https://discordapp.com/api/guilds/[GuildID]/members/[UserID]",
WebRequestMethods.Http.Put,
"{"access_token":"[Redacted]"}"//Option 2
);
}
Because I'd like to understand the intracacies of how this works, I'd prefer to know how to do this with ordinary Web requests (such as HttpWebRequest and WebClient, as opposed to using some OAuth library).
See Question&Answers more detail:
os