如何获取 spotify 端点 url 的 spotify 艺术家 ID?

How to get spotify artist id for the spotify endpoint url?

我正在尝试获取某位艺术家的前十首曲目,但我不知道如何获取该特定艺术家的 Spotify ID?这是终点。

GET /v1/artists/{id}/top-tracks

前提是这样,我应该可以输入歌手名,我应该可以得到前十首曲目的JSON数据。

function test() {
  var query = $('#searchArtist').val();
  console.log(query);
  $.ajax({
    url: 'https://api.spotify.com/v1/artists/' + query + '/top-tracks?country=IN',
    success: function(response) {
      console.log(response)
    }
  })
}
$('#search').on('click', test);

我应该先授权吗?因为我搜索了文档,取前十曲目数据不需要授权。

另外,让你知道。这里没有后端环境,如文档中所述,简单的数据获取不需要。

TZHX 说的。您需要两个单独的请求。 Javascript 在浏览器中不是我的主要环境。我会在 bash 中用 curl 和 jq 来解释。希望它足够清楚。

$ API_ARTIST_URL=$(curl -s 'https://api.spotify.com/v1/search?q=Daft+Punk&type=artist' | jq -r '.artists.items[0].href')

$ echo $API_ARTIST_URL
https://api.spotify.com/v1/artists/4tZwfgrHOc3mvqYlEYSvVi

$ curl -s "$API_ARTIST_URL/top-tracks?country=US" | jq -r '.tracks[].name'
Get Lucky - Radio Edit
One More Time
Instant Crush
Get Lucky
Lose Yourself to Dance
Around The World
Harder Better Faster Stronger
Doin' it Right
Something About Us
Give Life Back to Music

另一个细节。 ?country=IN 不会产生任何结果,因为 Spotify 尚未在印度推出。