const clientId= ‘XXXXXXXXXXXXXXXXXXXXXXX’;
const redirectUrl=‘http://localhost:3000/’;
let acessToken;
const Spotify = {
getAcessToken(){
if (acessToken){
return acessToken;
}
//check for access token match
const accessTokenMatch = window.location.href.match(/access_token=([^&])/);
const expiresInMatch = window.location.href.match(/expires_in=([^&])/);
if(accessTokenMatch && expiresInMatch){
acessToken = accessTokenMatch[1];
const expiresIn = Number(expiresInMatch[1]);
//this clears the parameters, allowing us to grab a new access token when it expires
window.setTimeout(() => accessToken = ' ', expiresIn * 1000);
window.history.pushState(`Access Token`, null, `/`);
return acessToken;
} else {
const accessUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&scope=playlist-modify-public&redirect_uri=${redirectUrl}`;{}
window.location = accessUrl;
}
},
search(term){
const accessToken = Spotify.getAcessToken();
return fetch(`https://api.spotify.com/v1/search?type=track&q=${term}`,{
headers: {
Authorization: `Bearer ${acessToken}`
}
}).then(response =>{ return response.json();
}).then(jsonResponse => {
if (!jsonResponse.tracks) {
return [];
}
return jsonResponse.tracks.items.map(track => ({
id: track.id,
name: track.name,
artist: track.artists[0].name,
album: track.album,
uri: track.uri
}));
});
},
}
From de course web Application engineer
Hi There, I am having trouble to understand what I am doing wrong here, following the step by step video, and yet stuck with this error, that I am not able to understand it!