Skip to content

Instantly share code, notes, and snippets.

@cdaz5
Created May 17, 2018 02:15
Show Gist options
  • Select an option

  • Save cdaz5/cfc12085a8d59ffac4ecf3ed03033c02 to your computer and use it in GitHub Desktop.

Select an option

Save cdaz5/cfc12085a8d59ffac4ecf3ed03033c02 to your computer and use it in GitHub Desktop.
savePlayList: (name, trackURIs) => {
if (!name || !trackURIs.length) {
return;
}
//Step 91: Create 3 variables, one for access token set to users token
//a header set to object with authorization parameter with user's token from Spotify's implicit grnat flow
//an empty variable for user ID.
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`, {
//pass a second argument that contains an object with parameters for headers, method & body
headers: headers,
method: 'POST',
body: JSON.stringify({ name: name })
});
})
.then(response => 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 })
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment