Hi! I get 401 error trying to use spotify api, if anyone could find what is wrong with my code i would appreciate so much!
Sporify.js :
const clientID = '*********';
const redirectUri = "http://localhost:3000/";
let accessToken;
const Spotify = {
getAccessToken() {
if(accessToken) {
return accessToken;
}
//check for acces token match
const accessTokenMatch = window.location.href.match(/acess_token=([^&]*)/);
const expiresInMatch = window.location.href.match(/expires_in=([^&]*)/);
if (accessTokenMatch && expiresInMatch) {
accessToken = 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, '/');
console.log(accessToken);
return accessToken;
} else {
const accessUrl = `https://accounts.spotify.com/authorize?client_id=${clientID}&response_type=token&scope=playlist-modify-public&redirect_uri=${redirectUri}`;
window.location.href = 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.artists[0].name,
album: track.album.name,
uri: track.uri
}));
})
},
savePlaylist(name, trackUris) {
if(!name || !trackUris) {
return;
}
const accessToken = Spotify.getAccessToken();
const headers = {Authorization: `Bearer ${accessToken}`};
let userId;
return fetch('https://api.spotify.com/v1/me', {headers: headers}
).then(response => {
return 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 => {return response.json()
}).then(jsonResponse => {
const playlistId = jsonResponse.id;
return fetch(`https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`,
{
headers: headers,
method: 'POST',
body: JSON.stringify({uris: trackUris})
})
})
}
}
export default Spotify;