Discord API users/@me 始终返回相同的用户对象,即使登录到另一个用户时也是如此

问题描述 投票:0回答:1

我正在尝试通过 Discord API OAuth2 身份验证流程(常规应用程序而不是机器人)获取用户电子邮件。

我可以成功完成 OAuth2 流程,并让用户允许我的应用程序查看他们的电子邮件。我将使用电子邮件 [email protected](第二个测试用户)下的测试帐户。

但是,即使我以完全不同的用户身份登录,并按照 OAuth2 流程获取访问令牌,api/v6/users/@me 端点始终返回我的 2 个测试用户的原始用户对象(甚至如果它甚至不应该访问该帐户,因为访问令牌是用于第二个测试用户的)。

下面我就带大家看看我使用的整个过程

使用第二个测试帐户的OAuth2授权URL

OAuth2 Authorization URL Using 2nd Test Account Screenshot

在链接的屏幕截图中,请注意此处请求的电子邮件是 [电子邮件受保护]

server.js(授权码回调)

这是我来自 OAuth2 授权的回调

server.get('/auth/discord/callback', function(req, res) {

    const discordUID = req.query.state;

    // Get the access token with email and identify scopes on the mmaines-cisco user
    axios.post(`https://discordapp.com/api/oauth2/token?grant_type=client_credentials&client_id=344539394373582849&client_secret=Cck2cchaqQiLCRASTXhcDwHZscNFLW7u&code=${req.query.code}&scope=email+identify&state=${req.query.state}`, {})
    .then(function(response) {
        console.log(response.data);

        res.cookie("access_token", response.data.access_token);
        res.redirect("/discord/tag-email/");

    }).catch(function(err) {
        console.log(err);
        res.status(500).send(err);
    });

});

已收到Access_Token

{ 
    access_token: 'loUOWKzOgUj45PSkbGoXUFgY6wQS52',
    token_type: 'Bearer',
    state: '347013221682511873',
    expires_in: 604800,
    scope: 'email identify' 
}

上面是来自 /api/oauth2/token 端点的响应,请注意 access_token 并注意状态密钥,该状态密钥携带验证其电子邮件访问权限的用户的 Discord userID

重定向页面

此页面是用户接受授权并授予对其电子邮件和用户信息的访问权限后最终到达的页面。在这里,我在客户端上运行 api 请求,以绝对确保 /users/@me 资源是他们的帐户

请注意,access_token 作为 cookie 传递给客户端(我也尝试将其作为 url 参数传递,并且具有相同的结果)

index.hbs

<h1>Thank You!</h1>
<h3>Your membership is being unlocked now!</h3>

{{access_token}}

<script>
    console.log(`Bearer {{access_token}}`);
    axios.get(`https://discordapp.com/api/v6/users/@me`, {
        headers: {
            "Authorization": `Bearer {{access_token}}`,
            "Content-Type": "application/x-www-form-urlencoded" 
        }
    })
    .then(function(response) {
        console.log("USER: ", response.data);
    })
    .catch(function(err) {
        console.log(err);
    });
</script>

用户授权并从 Discord API 接收到用户对象后索引路由的屏幕截图

ScreenShot of the recieved user object from the Discord API

请注意,usernameuser-idemail都与预期值不同(这些是我的主要测试帐户的值)。我已从所有不和谐客户端中的主要测试帐户注销,并作为我的第二个测试用户登录到网络客户端。

另请注意,打印出来的 access_token 与我们获得第二个测试帐户授权后从 api 收到的 access_token 相同

问题

我不确定为什么 api/v6/users/@me 端点返回一个用户对象,其中

  1. access_token 甚至不应该访问主测试帐户信息
  2. 未返回 api 调用时登录用户的用户信息

对于为什么会这样有什么想法吗?感谢您阅读本文,并提前感谢您的帮助。

discord
1个回答
0
投票

我知道已经有一段时间了,但我遇到了同样奇怪的问题,无论处理 OAuth 的用户如何,我总是从 Discord 收到相同的令牌。

看来我通过改变这个找到了解决方案:

axios.post(`https://discordapp.com/api/oauth2/token?grant_type=client_credentials..

进入这个:

axios.post(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code...

正如我们在屏幕截图中看到的,您在不和谐的 OAuth 授权 URL 参数中调用了“response_type=code”。然后 Discord 发回“authorization_code”而不是“client_credentials”。

© www.soinside.com 2019 - 2024. All rights reserved.