Javascript 我想用discord Oauth2显示用户名和头像
我想用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
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);
});
}