我正在尝试获取某位艺术家的前十名轨道,但我不知道如何获取该特定艺术家的 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 尚未在印度推出。
关于javascript - 如何获取 spotify 端点 url 的 spotify 艺术家 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41566971/