gpt4 book ai didi

node.js - Discord OAuth2 返回 '{"错误“: "access_denied"}'

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:19 25 4
gpt4 key购买 nike

我目前已经走进了 OAuth2 的世界,并对它有了一些的了解。我正在尝试制作一个应用程序,您可以在其中授权 Discord 帐户并让它自动邀请您加入公会/服务器。

我正在 Nginx 代理后面使用 Express Web 服务器,该代理已经配置好并且工作起来非常顺利。我使用名为“request”的 Node.JS 依赖项向 Discord 服务器发出发布请求。

当我允许应用程序 ( Discord Callback ) 时,以下代码将处理来自 Discord 的回调。回调将在 GET 查询中包含一个代码,该代码将被转发到 Discord token URL 以接收访问 token 。这个访问 token 允许应用程序对用户帐户执行某些操作,但事实是,它不起作用,我将在下面解释。 :-)

// 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_type”设置为“client_credentials”,它确实会返回一个有效的响应,这肯定证实了我最初认为我的服务器被 Discord 禁止 IP 的想法是不正确的。不幸的是,“client_credentials”不能满足我的目的,我需要“authorization_code”。

谢谢。 :-)

最佳答案

无需尝试制作自己的 Discord OAuth 应用程序,只需使用护照和护照不和谐即可授权您的应用程序。

Passport 可以轻松创建授权服务,并且广为人知。这是护照的链接:http://passportjs.org/

编辑:固定链接

关于node.js - Discord OAuth2 返回 '{"错误“: "access_denied"}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com