我将如何从Discord OAuth2中读取访问令牌

时间:2019-01-20 04:30:19

标签: node.js oauth-2.0 discord

我很难弄清楚如何去读取不和谐的oauth2令牌。这是我登录后给出的。我尝试将令牌设置为cookie。但是我不确定我将如何从那里阅读它。

discord.js

router.get('/callback', catchAsync(async (req, res) => {
    if (!req.query.code) throw new Error('NoCodeProvided');
    const code = req.query.code;
    const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
    const response = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
      {
        method: 'POST',
        headers: {
          Authorization: `Basic ${creds}`,
        },
      });
    const json = await response.json();
    res.cookie("access_token", json.access_token);
    res.redirect(`/?token=${json.access_token}`);
  }));

index.ejs

<script>
    axios.get(`https://discordapp.com/api/v6/users/@me`, {
        headers: {
            "Authorization": `Basic {token}`,
        }
    })
    .then(function(response) {
        console.log("Data: ", response.data);
    })
    .catch(function(err) {
        console.log(err);
    });
</script>

0 个答案:

没有答案