/RemainRejectAddiction
Created 2 years, 6 months ago...
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
async function spotifyLogin(){ // autho login function
window.open(`https://accounts.spotify.com/authorize?client_id={client_id}&response_type=code&scope=streaming&redirect_uri=http://localhost:5500`, 'Login with Spotify', 'width=800,height=600')
window.spotifyCallback = (payload) => {
fetch('https://api.spotify.com/v1/me', {
headers: {'Authorization': `Bearer ${payload}`}}).then(response => {
return response.json()
})
}
}
function newToken(){ // New Access Token
let xml = new XMLHttpRequest();
xml.open('POST', 'https://accounts.spotify.com/api/token', true);
xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xml.setRequestHeader('Authorization', `Basic ${btoa(client_id + ':' + client_secret)}`);
xml.send(`grant_type=authorization_code&code=${refresh}&redirect_uri=http://localhost:5500`);
xml.onload = () =>{
var data = JSON.parse(xml.responseText);
console.log(data)
if (data.access_token != undefined) set('spotifyPayload', data['access_token']);
if (data.refresh_token != undefined) set('spotifyRefresh', data['refresh_token']);
};
}