【问题标题】:Given the new Spotify web api authentication credentials, is it possible to run the api without a server?鉴于新的 Spotify web api 身份验证凭据,是否可以在没有服务器的情况下运行 api?
【发布时间】:2017-07-30 02:52:29
【问题描述】:

我正在尝试使用 node-spotify-api npm 包运行 spotify web api。为了需要这个包,我必须使用一个叫做browserify的东西,它允许我在浏览器中专门使用“require()”函数。我严格尝试在客户端运行 api。当我这样做时,这是我得到的错误 -

    // I have my client ID and client secret ID delcared in variables
    // Code below is suppose to execute the search function once the user enters a track

    var query = $("#query").val().trim();
     console.log(query)
    spotify.search({ type: 'track', query: query }, function(err, data) 
      {
      if (err) {
        return console.log('Error occurred: ' + err);
      }

    console.log(data); 

    });



// Once executed, I get the error below

Response to preflight request doesn't pass access control check: No 
'Access-Control-Allow-Origin' header is present on the requested 
resource. Origin 'null' is therefore not allowed access. If an opaque 
response serves your needs, set the request's mode to 'no-cors' to 
fetch the resource with CORS disabled.
bundle.js:120 Error occurred: RequestError: TypeError: Failed to fetch

我查看了错误,我正在查看的所有内容都将我重定向到在服务器端运行 api 的方向,如果可能的话,我不想这样做。关于如何严格运行这个客户端有什么想法吗?

【问题讨论】:

  • 请向我们展示您尝试提出的实际 Spotify 请求。您指向的特定错误是跨域访问错误,这意味着您尝试访问的站点不允许跨域访问,或者您没有以正确的方式发出请求以执行跨域请求。我们需要更多地了解您尝试发出的 API 请求,以便了解它是哪一个。
  • 我想知道这个Spotify API Web : OAuth without Node.js 是不是你要找的。​​span>
  • 刚刚用 spotify 电话更新了我的问题。至于 OAuth 链接,我查看了同一个 repo,据我了解,该示例可从服务器端操作。如果可能,我想将其作为前端应用程序运行并将其部署为前端应用程序。本质上,我想知道您是否可以将 node.js 用于前端应用程序。
  • 好吧,你不能将 node.js 用于在浏览器中运行的前端应用程序,因为 node.js 不在浏览器中运行。您必须编写基于浏览器的 Javascript 才能在浏览器中运行。例如,浏览器中没有http 模块。相反,您必须使用XMLHttpRequest 或fetch() 进行Ajax 调用。在浏览器中使用了 spotify 包装器。我想这可能就是你想要的。
  • 我发现了一个叫browserify的东西——browserify.org允许我在浏览器中运行node.js;特别允许我使用 require(" ") 我仍然遇到了这个与 spotify 相关的错误。错误是 spotify 函数中的 console.log。我最初使用 ajax 调用来运行 spotify web api,它运行完美,这是一个前端应用程序。自从 spotify 更改了对 api 的访问权限后,我现在在将其作为前端应用程序运行时遇到了问题。

标签: node.js api npm browserify spotify


【解决方案1】:

我认为您正在描述名为 Client Credentials 的身份验证流程,并且不,这是不可能的,因为 Spotify 拒绝在其身份验证令牌端点上允许 CORS。 See this issue on GitHub.

您需要使用 Implicit Grant flow 才能在浏览器中完全利用 API。

【讨论】:

  • FWIW,我认为 Spotify 拒绝在身份验证令牌端点上启用 CORS 并且也没有公共 API(公共意味着不需要身份验证的 API)是愚蠢的。他们迫使想要公开使用 API 的人承担服务器成本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2019-03-30
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
相关资源
最近更新 更多