1

所以我在开发一个 spotify 应用程序,试图让它与我的本地 Rails 应用程序 API 对话。当我尝试时,除了 req.status 0 之外,我什么也得不到。

我认为这可能是 spotify manifest.json 文件的问题,不允许 port:3000 继续您在所需权限中设置的 url,但它还在他们的文档中说明了以下内容。

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

如果您需要与外部 Web API 交谈,我们欢迎您,只要您遵守集成指南中设置的规则。请注意,当与 Web API 交谈时,请求将来自源 sp://$APPNAME(我们的示例中是 sp://tutorial)——确保您正在与之交谈的服务接受来自此类源的请求。

所以,我不确定 rails 是否设置为不允许这种事情,或者是否将端口放入所需的权限有问题,但我的请求

  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();

总是返回状态 0 作为他们的例子:

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();

至少会返回 403 响应。好像没有提出请求或其他什么?

有人知道会发生什么吗?

非常感激!

4

2 回答 2

2

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.

于 2012-08-04T09:40:35.523 回答
1

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!

于 2012-08-04T17:10:59.417 回答