admin管理员组

文章数量:1392050

I am trying to get the top ten tracks for an artist, but I don't know how to get Spotify id for that particular artist? this is the end point.

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

The premise is this, i should be able to type an artist name, and I should get the JSON data for the top ten tracks.

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

Am I supposed to first authorize? Because I searched the document and fetching the top ten tracks data doesn't require authorization.

Also, to let you know. There is no backend environment here, as mentioned in the docs, it is not required for simple data fetch.

I am trying to get the top ten tracks for an artist, but I don't know how to get Spotify id for that particular artist? this is the end point.

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

The premise is this, i should be able to type an artist name, and I should get the JSON data for the top ten tracks.

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

Am I supposed to first authorize? Because I searched the document and fetching the top ten tracks data doesn't require authorization.

Also, to let you know. There is no backend environment here, as mentioned in the docs, it is not required for simple data fetch.

Share Improve this question edited Jan 10, 2017 at 14:35 jooon 2,03115 silver badges26 bronze badges asked Jan 10, 2017 at 10:48 relentless-coderrelentless-coder 1,5463 gold badges22 silver badges42 bronze badges 1
  • 3 I guess you need to make two separate requests, one to get the artist id and then one to get the top tracks. – TZHX Commented Jan 10, 2017 at 10:52
Add a ment  | 

1 Answer 1

Reset to default 4

What TZHX said. You need two seperate requests. Javascript in the browser is not my main environment. I will explain with curl and jq in bash. Hopefully it is clear enough.

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

$ echo $API_ARTIST_URL
https://api.spotify./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

Another detail. ?country=IN will not yield any results, because Spotify has not launched in India yet.

本文标签: javascriptHow to get spotify artist id for the spotify endpoint urlStack Overflow