Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

axios - Spotify Web API - Issue Add items to playlist using Node.js

I am attempting to programmatically add Spotify tracks to a playlist, however, I am running into an issue.

If I attempt to add 6 or less tracks to a playlist, no tracks are added to the playlist and no errors are returned. In fact, I receive the expected response from Spotify - the snapshot_id with a status of 201 'Created'.

If I attempt to add more than 6 tracks at one time, all tracks up to the sixth to last get added to the playlist. For example, if I attempt to add 10 tracks to a playlist, only the first 4 will be added.

This is a NodeJS application. I have tried using various libraries for making http requests including node-fetch, axios, and request - all with the same outcome.

I have also tried using they old Spotify playlist endpoint to no avail.

Below are two attempts at adding tracks to playlist using two different endpoints using the node-fetch npm package. In both cases the request body has 25 tracks, but only the first 19 are added to the playlist.

fetch(`https://api.spotify.com/v1/playlists/${playlist_id}/tracks`, 
{ 
    method: 'post',
    body:'{"uris":["spotify:track:7zr0cqz7q0N3ED57wcEZVY","spotify:track:1JwqcbApkpl4PGeJZVq6KX","spotify:track:752es5q8G4DIqdJpJPVDwX","spotify:track:6qRHYEALF2n8AO79DcxfeY","spotify:track:6qS6HJ9ZllRugn6PjnUZZy","spotify:track:4XCflK0Q5WmpaQFsh6ly8C","spotify:track:0REN3SsECOCSKPkEqIEpYI","spotify:track:7xAUd0owJSCFMmmosEOU3d","spotify:track:2o3VdzVj1qRGJpLI5y2qMj","spotify:track:4XWmfmbu9NqU826rL0tecm","spotify:track:0RlCgU6jY77oHS9xX9mUB8","spotify:track:4RgPUr1n6bICbszd0cNfa2","spotify:track:0oKG1yGI9Zr0lbOxxd5zAS","spotify:track:6If7svQBuKPNwWQDQwxm9z","spotify:track:1YHkVmykQ0vM1LCtvklny2","spotify:track:4TyssfYytYjiNxKtfAhU6z","spotify:track:7tlwy1pLNMA8zTuDzNiwrZ","spotify:track:7mDnBgValNFrkjerIeur78","spotify:track:28fKJRaDVIvkprOAOfLw8i","spotify:track:3HmpazyhL0znLowq8EGePn","spotify:track:5bf6FEu70UlQXwaGimQd5p","spotify:track:4wpi5mdzD6lmO0qXkxNu7D","spotify:track:3U4MDMhKuSsPenF9ABTXkA","spotify:track:4htrElfXTiLZFUfciynXKB","spotify:track:6Tt19w2o7MnWttEILmefop"]}',
    headers: { 
         'Authorization': 'Bearer ' + access_token,
         'Content-Type': 'application/json' 
    } 
})
fetch(`https://api.spotify.com/v1/users/${user_id}/playlists/${playlist_id}/tracks`, 
{ 
    method: 'post',
    body:'{"uris":["spotify:track:7zr0cqz7q0N3ED57wcEZVY","spotify:track:1JwqcbApkpl4PGeJZVq6KX","spotify:track:752es5q8G4DIqdJpJPVDwX","spotify:track:6qRHYEALF2n8AO79DcxfeY","spotify:track:6qS6HJ9ZllRugn6PjnUZZy","spotify:track:4XCflK0Q5WmpaQFsh6ly8C","spotify:track:0REN3SsECOCSKPkEqIEpYI","spotify:track:7xAUd0owJSCFMmmosEOU3d","spotify:track:2o3VdzVj1qRGJpLI5y2qMj","spotify:track:4XWmfmbu9NqU826rL0tecm","spotify:track:0RlCgU6jY77oHS9xX9mUB8","spotify:track:4RgPUr1n6bICbszd0cNfa2","spotify:track:0oKG1yGI9Zr0lbOxxd5zAS","spotify:track:6If7svQBuKPNwWQDQwxm9z","spotify:track:1YHkVmykQ0vM1LCtvklny2","spotify:track:4TyssfYytYjiNxKtfAhU6z","spotify:track:7tlwy1pLNMA8zTuDzNiwrZ","spotify:track:7mDnBgValNFrkjerIeur78","spotify:track:28fKJRaDVIvkprOAOfLw8i","spotify:track:3HmpazyhL0znLowq8EGePn","spotify:track:5bf6FEu70UlQXwaGimQd5p","spotify:track:4wpi5mdzD6lmO0qXkxNu7D","spotify:track:3U4MDMhKuSsPenF9ABTXkA","spotify:track:4htrElfXTiLZFUfciynXKB","spotify:track:6Tt19w2o7MnWttEILmefop"]}',
    headers: { 
         'Authorization': 'Bearer ' + access_token,
         'Content-Type': 'application/json' 
    } 
})

Here are the response headers after making a request to add 6 tracks to a playlist using axios - no tracks are added:

  status: 201,
  statusText: 'Created',
  headers:
   { 'content-type': 'application/json; charset=utf-8',
     'cache-control': 'private, max-age=0',
     'x-robots-tag': 'noindex, nofollow',
     'access-control-allow-origin': '*',
     'access-control-allow-headers':
      'Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token',
     'access-control-allow-methods': 'GET, POST, OPTIONS, PUT, DELETE, PATCH',
     'access-control-allow-credentials': 'true',
     'access-control-max-age': '604800',
     'content-length': '80',
     'strict-transport-security': 'max-age=31536000',
     'x-content-type-options': 'nosniff',
     vary: 'Accept-Encoding',
     date: 'Wed, 27 Jan 2021 21:09:20 GMT',
     server: 'envoy',
     via: 'HTTP/2 edgeproxy, 1.1 google',
     'alt-svc': 'clear',
     connection: 'close' },
  config:
   { url:
      'https://api.spotify.com/v1/playlists/{PLAYLIST ID REDACTED}/tracks',
     method: 'post',
     data:
      '{"uris":["spotify:track:7zr0cqz7q0N3ED57wcEZVY","spotify:track:1JwqcbApkpl4PGeJZVq6KX","spotify:track:752es5q8G4DIqdJpJPVDwX","spotify:track:6qRHYEALF2n8AO79DcxfeY","spotify:track:6qS6HJ9ZllRugn6PjnUZZy","spotify:track:4XCflK0Q5WmpaQFsh6ly8C"]}',
     headers:
      { Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        Authorization:
         'Bearer {TOKEN REDACTED}',
        'User-Agent': 'axios/0.21.1',
        'Content-Length': 244 }
...

Lastly, I have attempted using the Spotify Developer Console to execute this same request and experience the same behavior. The snapshot id is returned, it looks like everything worked flawlessly until I actually check the playlist...still empty.

BUT, Spotify allows you to "Fill sample data" in their developer console. If I keep my request the same as it's been, and add their sample tracks into my request body at any position in the uris array, only those two tracks get added to my playlist!

Can someone help me out here? What am I missing?

question from:https://stackoverflow.com/questions/65928362/spotify-web-api-issue-add-items-to-playlist-using-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...