Hello, I’m Karina!
I need help for my project (Web Developing). So the case is, i’ve already finished all the 99 steps, and think i did it all right (no errors and any warnings), BUT !! On my “ready site” when I’m writing in the searchbar names of Artists and clicking “Search” the “Result” don’t work, nothing appears…also the same with “Save to Spotify”.
What I did wrong?
My project’s site: http://dreary-sack.surge.sh/
My Project’s Spotify.js: ```
const clientId = ‘’;
const redirectUri = ‘http://dreary-sack.surge.sh’;
let accessToken;
const Spotify = {
getAccessToken() {
if (accessToken) {
return accessToken;
}
const accessTokenMatch = window.location.href.match(/access_token=([^&]*)/);
const expiresInMatch = window.location.href.match(/expires_in=([^&]*)/);
if (accessTokenMatch && expiresInMatch) {
accessToken = accessTokenMatch[1];
const expiresIn = Number(expiresInMatch[1]);
window.setTimeout(() => accessToken = '', expiresIn * 1000);
window.history.pushState('Access Token', null, '/');
return accessToken;
} else {
const accessUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}_type=token&scope=playlist-modify-public&redirect_uri=${redirectUri}`;
window.location = accessUrl;
}
},
search(term) {
const accessToken = Spotify.getAccessToken();
return fetch(`https://api.spotify.com/v1/search?type=track&q=${term}`, {
headers: {
Authorization: `Bearer ${accessToken}`
}
}).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.artist[0].name,
uri: track.uri
}))
})
},
savePlaylist(name, trackUris) {
if (!name || !trackUris.length) {
return;
}
const accessToken = Spotify.getAccessToken();
const headers = {
Authorization: `Bearer ${accessToken}`
};
let userId;
return fetch('https://api.spotify.com/v1/me', {
headers: headers
}).then(response => response.json()).then(jsonResponse => {
userId = jsonResponse.id;
return fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
headers: headers,
method: 'POST',
body: JSON.stringify({
name: name
})
}).then(response => response.json()).then(jsonResponse => {
const playlistId = jsonResponse.id;
return fetch(`http://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`, {
headers: headers,
method: 'POST',
body: JSON.stringify({
uris: trackUris
})
})
})
});
}
}
export default Spotify