Search code examples
ruby-on-railsapiqueuespotify

Spotify API queue method returning 503 Service Unavailable error


I'm using Spotify developer API from a webapplication I'm developing in Ruby on Rails. For that I'm using a gem called rspotify. I modified it slightly in order to add the capability to queue a song which is offered by the Spotify API. Here is the API documentation from Spotify Dev: https://developer.spotify.com/console/post-queue/

Here is the original piece of code from rspotify with all existing (and working) calls (like play a song): https://github.com/guilhermesad/rspotify/blob/master/lib/rspotify/player.rb

And here is the piece of code I added:

def queue(device_id = nil, uri)
  url = "me/player/queue?uri=#{uri}"
  url = device_id.nil? ? url : "#{url}?device_id=#{device_id}"

  User.oauth_put(@user.id, url, {})
end

Unfortunately, all methods work but not this one. Here is an example of the call with the error I get (extract of a ruby call in the console):

> me = RSpotify::User.new(hash)
> player = RSpotify::Player.new(me)
> track = RSpotify::Track.search('Know')[0]
>
> player.play_track(track.uri)
 => nil
> player.queue(track.uri)
warning: Overriding "Content-Type" header "application/json" with "application/x-www-form-urlencoded" due to payload
Traceback (most recent call last):
        1: from (irb):82
RestClient::ServiceUnavailable (503 Service Unavailable)

Note that the call to method play_track works fine. Only the player.queue is a problem here.

I hope somebody can help me.

Thanks in advance


Solution

  • Problem is solved.

    The issue was coming from the fact that a PUT method was used instead of a POST. Here is the fixed code:

    def queue(device_id = nil, uri)
      url = "me/player/queue?uri=#{uri}"
      url = device_id.nil? ? url : "#{url}&device_id=#{device_id}"
      User.oauth_post(@user.id, url, {})
    end
    

    Hope it will save some time to others