【问题标题】:Access-Control-Allow-Origin denied spotify api [duplicate]Access-Control-Allow-Origin 被拒绝 spotify api [重复]
【发布时间】:2015-04-08 00:11:13
【问题描述】:

我正在尝试像这样访问 Spotify API 令牌:

$.ajax({
  url: "https://accounts.spotify.com/api/token",
  type: 'POST',
  contentType: "application/json; charset=\"utf-8\"",
  crossDomain: true,
  data: {
    grant_type: "authorization_code",
    code: code,
    redirect_uri: "http://www.bancadigital.com.br/spotifyteste/callback.html"
  },
  processData: false,
  dataType: "json",
  headers: {
    Authorization: "Basic " + utf8_to_b64(key)
  },
  success: function( response ) {
    alert(response.access_token);
  },
});

但服务返回以下错误:

XMLHttpRequest 无法加载 https://accounts.spotify.com/api/token。请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://www.bancadigital.com.br' 因此不允许访问。

有人知道我如何访问该服务吗?

【问题讨论】:

标签: javascript jquery rest spotify


【解决方案1】:

https://accounts.spotify.com/api/token 的请求需要在服务器端进行,而不是作为 AJAX 请求。

这样您的key(包含您的应用程序的凭据)将不会被暴露。此外,Spotify 服务器将能够将请求与访问令牌一起重定向到 redirect_uri

另一种方法是使用implicit grant flow,您可以在其中运行所有客户端,但您不会获得刷新令牌。

我建议您查看Spotify Web API Authorization Guide、查看the GitHub repo with auth examples 并查看libraries and wrappers,它们可以更轻松地实施 OAuth 流程。

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2013-01-20
    • 2016-04-15
    • 2012-08-16
    相关资源
    最近更新 更多