简体   繁体   中英

rails responding to cross domain request developing locally, spotify app development

So Im messing around with developing a spotify app, trying to get it to talk to my local rails application API. I cant get anything other than a req.status 0 when I try it.

I think its either a problem with the spotify manifest.json file, not allowing the port:3000 to go on the url you set in required permissions, but it also says the following in their documentation.

https://developer.spotify.com/technologies/apps/tutorial/

If you need to talk to an outside web API you're welcome to, as long as you abide by the rules set in the Integration Guidelines. Please note that when talking with a web API, the requests will come from the origin sp://$APPNAME (so sp://tutorial for our example) - make sure the service you are talking to accepts requests from such an origin.

So, Im not sure if rails is set to not allow this sort of thing, or if its an issue with the putting the port into the required permissions, but my request

  var req = new XMLHttpRequest();
  req.open("GET", "http://127.0.0.1:3000/api/spotify/track/1.json", true);
  console.log(req);
  req.onreadystatechange = function() {

  console.log(req.status);
  console.log(req.readyState);
  if (req.readyState == 4) {
    if (req.status == 200) {
       console.log("Search complete!");
       console.log(req.responseText);
    }
    }
};

req.send();

Always returns status 0 where as their example:

var req = new XMLHttpRequest();
req.open("GET", "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=" + city + "&api_key=YOUR_KEY_HERE", true);

req.onreadystatechange = function() {

    console.log(req.status);

    if (req.readyState == 4) {
      console.log(req);           
        if (req.status == 200) {
            console.log("Search complete!");
            console.log(req.responseText);
        }
    }
};

req.send();

Will return a 403 response at least. its like the request is not being made or something?

Anyone have any idea what might be going on?

Much appreciated!

When talking to external services from a Spotify App, even if they're running on your local machine, you need to make sure that two things are in place correctly:

  • The URL (or at least the host) is in the RequiredPermissions section of your manifest. Port doesn't matter. http://127.0.0.1 should be fine for your case.

  • The server is allowing the sp://your-app-id origin for requests, as noted in the documentation you pasted in your question. This is done by setting the Access-Control-Allow-Origin header in your service's HTTP response. People often set it to Access-Control-Allow-Origin: * to allow anything to make requests to their service.

Thanks for help, I got it figured out, I think it was multiple things, with one main Im an idiot moment for not trying that earlier

First off, I had to run rails on port 80, as obviously if Im accessing my site from 127.0.0.1:3000, thats not going to work if spotify app is requesting 127.0.0.1 unless I can load that directly in the browser, which you cannot unless you run on 80. That is done via

rvmsudo rails server -p 80

Need to use rvmsudo because changing port requires permissions.

Next I had to set access controll allow origin as noted above, that can be done in rails 3 by adding before filter to your app controller as follows.

class ApplicationController < ActionController::Base

  logger.info "I SEE REQUEST"

  before_filter :cor

  def cor
    headers["Access-Control-Allow-Origin"] = "*"
    headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE}.join(",")
    headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",")
    head(:ok) if request.request_method == "OPTIONS"
  end
end

Finally, and most importantly (sigh), you cant just righclick and reload your spotify app when you make changes to your manifest file, exit spotify completely and restart it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM