尽管访问令牌明显有效,但不和谐添加公会成员 401 错误 [英] Discord Add Guild Member 401 Error Despite Apparently Valid Access Token

查看:20
本文介绍了尽管访问令牌明显有效,但不和谐添加公会成员 401 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Discord 的 API 的新手,我正在处理一个需要能够以编程方式添加公会成员的项目.我已经学会了如何获取授权代码(带有标识和 guilds.join 范围),将其兑换为访问令牌,并获取用户 ID.最后一步是使用访问代码和用户ID添加公会.这个命令在这里有详细说明:

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/公会#add-guild-member

看来我需要向这个 URL 发送 PUT 请求:

It seems I need to send a PUT request to this URL:

https://discordapp.com/api/guilds/[GuildID]/members/[用户ID]

但这会导致以下响应:

{"code": 0, "message": "401: Unauthorized"}

{"code": 0, "message": "401: Unauthorized"}

我尝试在授权标头中包含访问令牌:

I have tried including the access token in the Authorization header:

授权:承载 [已编辑]

Authorization: Bearer [Redacted]

我还尝试向请求添加 JSON 正文:

I've also tried adding a JSON body to the request:

{"access_token":"[已编辑]"}

{"access_token":"[Redacted]"}

都没有奏效.不出所料,同时使用两者也不起作用.

Neither has worked. Unsurprisingly, using both at the same time hasn't worked either.

我想知道这是否是权限问题,但 Discord 确认我拥有 guilds.join 范围.这是我在为访问令牌交换授权码时收到的 JSON:

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"}

{"access_token": "[Redacted]", "token_type": "Bearer", "expires_in": 604800, "refresh_token": "[Redacted]", "scope": "identify guilds.join"}

识别范围有效,因为我能够检索用户及其 ID.但是 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.

我在下面包含了一些测试代码.我已标记选项 1"和选项 2"行以表示我通常不会在同一请求中同时使用这两种访问代码方法.但正如我之前提到的,我确实尝试了两者,但仍然出现 401 错误.

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
    );
}

因为我想了解它是如何工作的,所以我更想知道如何使用普通的 Web 请求(例如 HttpWebRequest 和 WebClient,而不是使用某些 OAuth 库)来做到这一点.

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]"}"
    );
}

请注意,我将内容类型从application/x-www-form-urlencoded"更改为application/json".我从使用Bearer"的授权标头更改为Bot",并且我正在使用我的机器人的令牌.我使用与以前相同的访问令牌.如果没有更好的解决方案,我打算接受这个答案.

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.

这篇关于尽管访问令牌明显有效,但不和谐添加公会成员 401 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆