gpt4 book ai didi

node.js - 为什么这有效? Spotify API 调用node.js

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:49 27 4
gpt4 key购买 nike

我是 Node 新手,正在通过 Node.js 进行 API 调用,但我有点困惑为什么它会起作用。我已经通过 Node 轻松完成了其他 API 调用,因为很容易弄清楚如何定位各个字段等。但我从未获得与Spotify API 的链接,并且很困惑 data.tracks.items.artists.name 是如何给出的我是艺术家的名字?

我知道这是一个无知的问题,但我真的很想了解它是如何工作的,而不仅仅是让它发挥作用。

function song() {
var nodeArgs = process.argv;
var SongName = "";
for (var i = 3; i < nodeArgs.length; i++) {
if (i > 3 && i < nodeArgs.length) {
SongName = SongName + "+" + nodeArgs[i];
}
else {
SongName += nodeArgs[i];
}
}

var Spotify = require('node-spotify-api');
var spotify = new Spotify({
id: "id",
secret: "secret"
});

spotify.search({ type: 'track', query: SongName, limit: 1 }, function (err, data) {
if (err) {
SongName = "";
console.log("Artist: " + songData.artists[0].name);
console.log("Song Title: " + songData.name);
console.log("Preview Track: " + songData.preview_url);
console.log("Album: " + songData.album.name);
song();
}

for (var i = 0; i < data.tracks.items.length; i++) {
var songData = data.tracks.items[i];
console.log("Artist: " + songData.artists[0].name);
console.log("Song Title: " + songData.name);
console.log("Preview Track: " + songData.preview_url);
console.log("Album: " + songData.album.name);
}
});
}

最佳答案

简短回答 -Track api 端点使用对象模型进行响应,该模型还包含艺术家对象 - 这是艺术家对象的数组,其中艺术家对象包含键名称

引用:https://developer.spotify.com/documentation/web-api/reference/tracks/get-track/

来自他们的 API 文档

GET https://api.spotify.com/v1/tracks/{id}

响应对象包含

KEY VALUE       | TYPE                   | VALUE DESCRIPTION
---
artists | an array of simplified | The artists who performed the track.
| artist objects | information about the artist.

艺术家对象

artist object (simplified)

KEY VALUE | TYPE | VALUE DESCRIPTION
---
external_urls | an external URL object | Known external URLs for this artist.
href | string | A link to the Web API endpoint providing full details of the artist.
id | string | The Spotify ID for the artist.
name | string | The name of the artist
type | string | The object type: "artist"
uri | string | The Spotify URI for the artist.

关于node.js - 为什么这有效? Spotify API 调用node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com