Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我想用discord Oauth2显示用户名和头像_Javascript_Html_Oauth 2.0_Discord - Fatal编程技术网

Javascript 我想用discord Oauth2显示用户名和头像

Javascript 我想用discord Oauth2显示用户名和头像,javascript,html,oauth-2.0,discord,Javascript,Html,Oauth 2.0,Discord,我想用discord Oauth2显示一个用户名和头像。我也写了代码,但是用户名会显示几秒钟,然后如果你刷新页面,它也会消失。那么我该如何解决这个问题呢 I want the username and avatar to be displayed forever. 这是我的代码: let params = new URLSearchParams(window.location.search); let info = document.getElementById('info'); if (p

我想用discord Oauth2显示一个用户名和头像。我也写了代码,但是用户名会显示几秒钟,然后如果你刷新页面,它也会消失。那么我该如何解决这个问题呢

I want the username and avatar to be displayed forever.
这是我的代码:

let params = new URLSearchParams(window.location.search);
let info = document.getElementById('info');

if (params.has('code')) {
    let code = params.get('code');

    fetch('https://discord.com/api/oauth2/token', {
        method  : 'POST',
        headers : {
            'Content-Type' : 'application/x-www-form-urlencoded'
        },
        body    : new URLSearchParams({
            client_id     : '###############3',
            client_secret : '##############',
            grant_type    : 'authorization_code',
            code          : code,
            redirect_uri  : 'http://127.0.0.1:5501/public/'
        })
    })
        .then((res) => res.json())
        .then(function(res) {
            const ref = `${res.refresh_token}`;

            fetch('https://discord.com/api/users/@me', {
                headers : {
                    authorization : `${res.token_type} ${res.access_token}`
                }
            })
                .then((res) => res.json())
                .then(function(res) {
                    const username = res.username;
                    info.innerHTML = username;
                });
        })
        .then(function(ref) {
            setInterval(() => {
                fetch('https://discord.com/api/oauth2/token/revoke', {
                    method  : 'POST',
                    headers : {
                        'Content-Type' : 'application/x-www-form-urlencoded'
                    },
                    body    : new URLSearchParams({
                        client_id     : '################',
                        client_secret : '#############3',
                        grant_type    : 'refresh_token',
                        refresh_token : ref
                    })
                });
            }, 100000);
        });
}