Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

c# - Discord Add Guild Member 401 Error Despite Apparently Valid Access Token

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...