#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 (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);
});
}