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).
I am answering my own question as I may have figured this out. It works when I execute the below:
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Headers.Add(HttpRequestHeader.Authorization, "Bot [Redacted]");
string output = client.UploadString
(
"https://discordapp.com/api/guilds/[GuildID]/members/[UserID]",
WebRequestMethods.Http.Put,
"{\"access_token\":\"[Redacted]\"}"
);
}
Notice that I changed from a content type of "application/x-www-form-urlencoded" to "application/json." I changed from using an Authorization header of "Bearer" to "Bot," and I am using my bot's token. I am using the same access token as before. I plan to accept this answer if no better solutions come in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With