Created
May 17, 2018 02:15
-
-
Save cdaz5/cfc12085a8d59ffac4ecf3ed03033c02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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