Sorry for the confusing title. I'm working on a project that compiles a number of artists' top 5 songs into a playlist for a user. I'm running into a roadblock in the final step of the process and I'm stuck.
My access token works when I search for artists' top five tracks and works when I create a playlist. I can verify receiving the track spotify ids and see the playlist is created on my Spotify app, but I'm hitting a 401 error when I try to add tracks to the playlist.
I am using passport-spotify for auth, and the scope is set to ["user-read-private, playlist-modify-private, playlist-modify-public"].
Since I could be adding a large number of songs to the playlist depending on the number of artists, I'm passing the song uris into the body of the axios request. Here's the documentation for adding tracks to a playlist.
I can verify in the error object that the access token and uris are passing into the post call.
I'll post my code for the addTracks axios call below, and I'm more than happy to share information in the error object as well. If you would like to see the auth code, I will also post that.
Thanks for taking a look!
const addTracks = (req, res) => {
let chunk = 100;
let result = [];
console.log('FINAL TRACKS: ', finalTracks);
console.log('addTracks REQ.BODY: ', req.body);
console.log('addTracks REQ.USER: ', req.user);
for (let i = 0; i < finalTracks.length; i += chunk) {
result.push(finalTracks.slice(i, i + chunk));
console.log('result: ', result);
}
result.map(chunkArr => {
axios
.post(
`https://api.spotify.com/v1/playlists/${req.body.playlist.id}/tracks`,
{
headers: {
Authorization: `Bearer ${req.user.accessToken}`,
'Content-Type': 'application/json'
}
},
{
body: {
uris: JSON.stringify(chunkArr)
}
}
)
.then(resp => {
// console.log(resp);
// res.status(200).json(resp);
console.log('awaiting promise resolution');
})
.catch(err => console.log('addTracks > result.map catch: ', err));
});
};
No comments:
Post a Comment