Spotify - 搜索艺术家

时间:2021-01-01 17:46:39

标签: javascript html css api spotify

我遇到过这个演示http://jsfiddle.net/JMPerez/0u0v7e1b/ 即使用 Spotify API 搜索艺术家。 我已经尝试了很多尝试,但无法使其正常工作。 关于这如何工作的任何指示?

来自演示的 HTML 代码:

<div class="container">
<h1>Search for an Artist</h1>
<p>Type an artist name and click on "Search". Then, click on any album from the results to play 30 seconds of its first track.</p>
<form id="search-form">
    <input type="text" id="query" value="" class="form-control" placeholder="Type an Artist Name"/>
    <input type="submit" id="search" class="btn btn-primary" value="Search" />
</form>
<div id="results"></div>

1 个答案:

答案 0 :(得分:0)

此代码示例不起作用的原因是未提供令牌。如果您尝试访问 https://api.spotify.com/v1/search,则该站点会返回:

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

您可以在标头中附加令牌作为授权,类似于:

$.ajax({
   url: 'https://api.spotify.com/v1/search',
   headers: {
      'Authorization': 'Bearer <token>'
   },
   success: function (result) {
       // CallBack(result);
   },
});

要获得开发者令牌,您可以查看 Spotify 的文档:https://developer.spotify.com/documentation/general/guides/authorization-guide/

有关使用 Spotify 的 Web API 的更多信息,您还可以查看他们的快速入门指南:https://developer.spotify.com/documentation/web-api/quick-start/

相关问题