Discord OAuth代码使用

use*_*179 6 c# oauth httpwebrequest oauth-2.0 discord

我对使用Discord API与Discord接口感兴趣.我会将他们的文档描述为"稀疏",但也许我只是没有找到正确的位置.我的大多数信息来自此页面:

https://discordapp.com/developers/docs/topics/oauth2

我已经设置了我的Discord公会和应用程序(甚至是机器人,这可能是不必要的).我的具体计划是允许用户授予我的网站权限,将其添加到私人Discord公会/服务器.我在我的网站页面上有一个引用此URL的超链接:

https://discordapp.com/api/oauth2/authorize?client_id=[ClientID]&scope=guilds.join&response_type=code&redirect_uri=[RedirectURI]

这部分似乎运作良好.用户批准该请求.然后,用户将在查询字符串中使用"代码"键值对发送回我的站点.我认为这段代码就是所谓的"授权代码".那么如何使用此授权码将用户添加到我的公会?我在Discord网站上找到了这个页面:

https://discordapp.com/developers/docs/resources/guild#add-guild-member

从该页面我可以看到我需要启动此URL的PUT:

https://discordapp.com/api/guilds/ {guild.id}/members/{user.id}

但我不知道{user.id}.我只有一个授权码.

它还说,"...如果你拥有guilds.join范围的用户,你有一个有效的oauth2访问令牌." 我没有访问令牌.同样,我只有一个授权码.

所以在我看来,我需要以某种方式交换此授权码以获取访问令牌和用户ID.有人能告诉我该怎么做吗?我一直在尝试使用以下URL,但我不知道使用什么方法(GET,POST等)或发送它的参数:

https://discordapp.com/api/oauth2/token

因为我想了解其工作方式的内在性,我更愿意知道如何使用普通的Web请求(例如HttpWebRequest和WebClient,而不是使用某些OAuth库).

更新

我决定阅读(有选择地)这个RFC:

https://tools.ietf.org/html/rfc6749#section-4.1.3

我已将我认为最合适的部分联系起来.似乎正确的过程是向以下URL和参数发送POST请求:

https://discordapp.com/api/oauth2/token

grant_type = authorization_code&代码= [AuthorizationCode]&REDIRECT_URI = [RedirectURI]&CLIENT_ID = [客户端ID]

这似乎也符合彼得G的答案.不幸的是,此请求因401(未授权)错误而失败.所以我认为这是一个死路一条.我已经尝试了几次,但希望有一个解决方案.我收到了这个回复机构:

{"error":"invalid_client"}

我收到了这些标题:

连接:关闭

Pragma:没有缓存

严格运输安全:max-age = 31536000; includeSubDomains

Alt-Svc:清楚

CF-RAY:[RedactedJustInCase]

内容长度:27

缓存控制:无存储

Content-Type:application/json

日期:2017年4月7日星期五01:12:19 GMT

Set-Cookie:__ cffidid = [RedactedJustInCase]; expires =周六,07-Apr-18 01:12:19 GMT; 路径= /; 域= .discordapp.com; 仅Http

服务器:cloudflare-nginx

通过:1.1谷歌

Pet*_*r G 5

就获取 OAuth 令牌而言,您已经差不多完成了。您只需使用链接的文档中列出的其他 URL,https://discordapp.com/api/oauth2/token。使用以下参数发布到它:https://discordapp.com/api/oauth2/token?client_id=[ClientID]&grant_type=authorization_code&code=[AuthorizationCode]&redirect_uri=[RedirectURI]&client_secret=[Secret]其中是AuthorizationCode返回第一个 URL 和 是Secret您首次注册应用程序时获得的客户端密钥。

这应该会让您在响应正文中取回客户端令牌(以及令牌过期所需的时间)。至于获取 User 对象,您需要将范围添加identify到第一个请求,以便您可以使用令牌来调用https://discordapp.com/developers/docs/resources/user#get-current-user(如果链接打破,它是GET users/@me)。该 API 将以 JSON 形式返回 User 对象。

最后,您可以使用刚刚获得的用户对象,通过 PUT-ing 到https://discordapp.com/api/guilds/[guild.id]/members/[user.id]来添加用户。

When using the APIs after getting the client token (the ones to get the user object and put the user in the guild), you need to put the token in the HTTP request under the authorization header using the Bearer auth scheme. Basically, that means the header should be set to "Bearer TOKEN_HERE". You should also use content-type "application/x-www-form-urlencoded" if you weren't already using it.

If there's anything you don't understand here, I strongly suggest reading about oauth from the source RFC (don't worry, these two sections are short): getting an auth code, getting a token, authenticating with Bearer scheme. Them's the breaks when you don't use a library.