我目前已经走进了OAuth2的世界
一些
对它的理解。我正在尝试制作一个应用程序,您可以授权Discord帐户,并让它自动邀请您加入公会/服务器。
我在一个Nginx代理后面使用一个Express web服务器,这个代理已经配置好了,工作起来很有魅力。我使用节点。JS依赖项调用“request”向Discord服务器发出post请求。
当我允许应用程序时(
Discord Callback
),下面的代码将处理来自Discord的回调。回调将在
获取
将转发到Discord令牌URL以接收访问令牌的查询。这个访问令牌允许应用程序对用户帐户执行操作,问题是,它不起作用,我将在下面解释。:-)
// there would be another variable here named 'state'
// which essentially is another security feature but this
// definitely works. The state gets calledback from the
// discord authorisation which is a hashed cookie
var clientId = '123' // Discord client ID (string to not lose accuracy)
var clientSecret = 'secretsauce' // Discord secret key
var code = req.query.code // received on router event (express)
var discordTokenURL = 'https://discordapp.com/api/oauth2/token'
var redirectUri = 'https://myapp.com/callback/discord' // redirect for when I allow my app to look at my data, or even when I refuse it
// dependency to make request
var request = require('request')
// post the data to the discord server
request.post({
url: discordTokenURL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: { grant_type: 'authorization_code', client_id: clientId, client_secret: clientSecret, code: code, redirect_uri: redirectUri }
},
function (err, httpResponse, body) {
if (err) {
return console.error('server down or other error') // only errors when Discord server is down
}
res.send(body) // returns the server response ({"error": "access_denied"})
})
代码成功执行,没有任何错误。在Discord OAuth2文档中,我应该收到如下响应:
{
"access_token": "6qrZcUqja7812RVdnEKjpzOL4CvHBFG",
"token_type": "Bearer",
"expires_in": 604800,
"refresh_token": "D43f5y0ahjqew82jZ4NViEr2YafMKhue",
"scope": "identify"
}
不幸的是,我有一个可爱的,看起来像这样:
{"error": "access_denied"}
如果有任何帮助,我将不胜感激,我已经阅读了这么多文章,并多次阅读了API文档,但什么都没有解决。我发现,如果我将“grant\u type”设置为“client\u credentials”,它确实会返回一个有效的响应,这肯定证实了我最初认为我的服务器被IP禁止脱离Discord的想法是错误的。很遗憾,“client\u credentials”不符合我的目的,我需要“authorization\u code”
谢谢。:-)