jishi / node-sonos-http-api

An HTTP API bridge for Sonos easing automation. Hostable on any node.js capable device, like a raspberry pi or similar.
http://jishi.github.io/node-sonos-http-api/
MIT License
1.84k stars 463 forks source link

Spotify resulting in different errors #728

Open denbue opened 4 years ago

denbue commented 4 years ago

Hi, first off, I was able to recreate the issue with fresh installs on linux, mac, docker and non-docker. System parameters like /clearqueue or /zones etc are all working fine.

I am trying to use the spotify feature to add songs to the queue and to play. Spotify credentials are set correctly in settings.json.

It used to work until a couple months ago and I can't get it to work anymore.

It's the same issue for track, album and playlist. Here is an example with an album for {now/queue/next}:

http://192.168.0.91:5005/Hallway/spotify/now/spotify:album:0JKjuvDUPrnlblAZyB1Aje
=
Error: Got status 500 when invoking /MediaRenderer/AVTransport/Control
    at Object.invoke (/node-sonos-http-api-master/node_modules/sonos-discovery/lib/helpers/soap.js:99:10)
    at Player.addURIToQueue (/node-sonos-http-api-master/node_modules/sonos-discovery/lib/models/Player.js:616:15)
    at promise.then (/node-sonos-http-api-master/lib/actions/spotify.js:37:33)

http://192.168.0.91:5005/Hallway/spotify/queue/spotify:playlist:37i9dQZF1DWSNmwgf7Nv11
=
Error: Got status 500 when invoking /MediaRenderer/AVTransport/Control
    at Object.invoke (/node-sonos-http-api-master/node_modules/sonos-discovery/lib/helpers/soap.js:99:10)
    at Player.addURIToQueue (/node-sonos-http-api-master/node_modules/sonos-discovery/lib/models/Player.js:616:15)
    at Object.spotify (/node-sonos-http-api-master/lib/actions/spotify.js:28:31)
    at handleAction (/node-sonos-http-api-master/lib/sonos-http-api.js:117:35)
    at HttpAPI.requestHandler (/node-sonos-http-api-master/lib/sonos-http-api.js:94:21)
    at /node-sonos-http-api-master/server.js:50:13
    at Server.finish (/node-sonos-http-api-master/node_modules/node-static/lib/node-static.js:111:13)
    at finish (/node-sonos-http-api-master/node_modules/node-static/lib/node-static.js:170:14)
    at /node-sonos-http-api-master/node_modules/node-static/lib/node-static.js:144:17
    at FSReqWrap.oncomplete (fs.js:123:15)

http://192.168.0.91:5005/Hallway/spotify/next/spotify:playlist:37i9dQZF1DWSNmwgf7Nv11
=
Error: Got status 500 when invoking /MediaRenderer/AVTransport/Control
    at Object.invoke (/node-sonos-http-api-master/node_modules/sonos-discovery/lib/helpers/soap.js:99:10)
    at Player.addURIToQueue (/node-sonos-http-api-master/node_modules/sonos-discovery/lib/models/Player.js:616:15)
    at Object.spotify (/node-sonos-http-api-master/lib/actions/spotify.js:44:31)
    at handleAction (/node-sonos-http-api-master/lib/sonos-http-api.js:117:35)
    at HttpAPI.requestHandler (/node-sonos-http-api-master/lib/sonos-http-api.js:94:21)
    at /node-sonos-http-api-master/server.js:50:13
    at Server.finish (/node-sonos-http-api-master/node_modules/node-static/lib/node-static.js:111:13)
    at finish (/node-sonos-http-api-master/node_modules/node-static/lib/node-static.js:170:14)
    at /node-sonos-http-api-master/node_modules/node-static/lib/node-static.js:144:17
    at FSReqWrap.oncomplete (fs.js:123:15)

my settings.json:

{
  "ip": "192.168.0.91",
  "port": "5005",
  "spotify": {
    "clientId": "b0f90b49ae1d4ca98e72ea0004e3_XXX",
    "clientSecret": "d2c7528701c542cf937f9c0390e_XXX"
    },
 }

my preset:

{
  "players": [
    {
      "roomName": "Hallway",
      "volume": 10
    },
    {
      "roomName": "Living Room",
      "volume": 10
    }
  ],
  "playMode": {
    "shuffle": true,
    "repeat": "all",
    "crossfade": false
  },
  "pauseOthers": false,

}
AlejandroSossa commented 4 years ago

Hello,

I have the same issue and the only way that I found to play a song from Spotify was:

http://localhost:5005/"Zone"/SetAVTransportURI/x-sonos-spotify:spotify:track:1WSGTYKLEpKfNQYYW5Oymt?sid=9&sn=12, but it is not the correct way to do that action, because I cant see all the information of the song.

Could you find the solution?

denbue commented 4 years ago

Hi, Glad to hear I’m not the only one with the issue. I haven’t looked at it but is there a way to enable a debug mode?

AlejandroSossa commented 4 years ago

Hi, I found a way to capture the traffic and this is the difference between the SONOS App and the API:

Correct way:

0
  <EnqueuedURI>x-sonos-spotify:spotify%3atrack%3a0fea68AdmYNygeTGI4RC18?sid=9&amp;flags=8224&amp;sn=15</EnqueuedURI>
  <EnqueuedURIMetaData>&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;&lt;item id=&quot;10032020spotify%3atrack%3a0fea68AdmYNygeTGI4RC18&quot; parentID=&quot;00020000track:la cancion&quot; restricted=&quot;true&quot;&gt;&lt;dc:title&gt;LA CANCI..N&lt;/dc:title&gt;&lt;upnp:class&gt;object.item.audioItem.musicTrack&lt;/upnp:class&gt;&lt;upnp:album&gt;OASIS&lt;/upnp:album&gt;&lt;dc:creator&gt;J Balvin&lt;/dc:creator&gt;&lt;upnp:albumArtURI&gt;https://i.scdn.co/image/ab67616d0000b2734891d9b25d8919448388f3bb&lt;/upnp:albumArtURI&gt;&lt;r:albumArtist&gt;J Balvin&lt;/r:albumArtist&gt;&lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON2311_X_#Svc2311-0-Token&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;</EnqueuedURIMetaData>
  <DesiredFirstTrackNumberEnqueued>1</DesiredFirstTrackNumberEnqueued>
  <EnqueueAsNext>1</EnqueueAsNext>
</u:AddURIToQueue>

</s:Body> </s:Envelope>

Incorrect:

0
  <EnqueuedURI>x-sonos-spotify:spotify%3Atrack%3A0fea68AdmYNygeTGI4RC18?sid=12&amp;flags=32&amp;sn=1</EnqueuedURI>
  <EnqueuedURIMetaData>&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt; &lt;item id=&quot;00030020spotify%3Atrack%3A0fea68AdmYNygeTGI4RC18&quot; restricted=&quot;true&quot;&gt;&lt;upnp:class&gt;object.item.audioItem.musicTrack&lt;/upnp:class&gt;&lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON3079_X_#Svc3079-0-Token&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;</EnqueuedURIMetaData>
  <DesiredFirstTrackNumberEnqueued>2</DesiredFirstTrackNumberEnqueued>
  <EnqueueAsNext>1</EnqueueAsNext>
</u:AddURIToQueue>

</s:Body> </s:Envelope>

The difference is: EnqueuedURIMetaData

I dont know how to implement that solution.

AlejandroSossa commented 4 years ago

The issue was the token number. I manually entered the value and it worked, but it should work automatically.

If you open "Spotify.js" and find: SA_RINCON${serviceType}X#Svc${serviceType}-0-Token

You should change it for numbers: SA_RINCON2311X#Svc2311-0-Token

denbue commented 4 years ago

Yeah you are right, when you change 3079 to 2311 in the above code snippet, it works again. I'm trying to find out where the 3079 originally comes from?

jishi commented 4 years ago

Previously, Spotlfy used different service identifiers in different regions, where 2311 (sn=9) was used in EU, and 3079 was used in US (I think).

It uses a Sonos system call to get available music services and fetches the one that is listed there and called "Spotify".

Please invoke /services against the API and also tell me which country you live in, maybe we can find what is going on.

denbue commented 4 years ago

Yeah, I saw this in #306

My /services is Spotify: { id: 12, capabilities: 1116691, type: 3079 }

The Spotify account is EU, and the Sonos was set up as an US account but I just switched it to EU and restarted the sonos speaker and reinstalled the app. It still says 3079.

denbue commented 4 years ago

Re-invoking the /services changed my Spotify type to EU (2311) now. Not sure if my mixed countries were the issue, but it seems solved now.

denbue commented 4 years ago

Just as a follow up: I'm using docker-compose to update my containers every night. This seems to reset the region of this package every time back to 3079 and I have to invoke /services after each removal/pull. Not sure if this is an issue of this package or the docker-container?