【问题标题】:Why does this work? Spotify API call node.js为什么这行得通? Spotify API 调用 node.js
【发布时间】:2019-01-23 04:01:21
【问题描述】:

我是 node 新手,正在通过 node.js 进行 API 调用,我有点困惑为什么会这样。我已经通过节点轻松完成了其他 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);
    }
});
}

【问题讨论】:

标签: node.js spotify


【解决方案1】:

简短回答 - track api 端点以 Object Model 响应,其中还包含艺术家对象 - 这是艺术家对象的数组,其中艺术家对象包含键 name

参考: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.

【讨论】:

  • 知道链接帮助很大。也许是睡眠不足,但我想我在阅读文档时掩盖了这一点。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
相关资源
最近更新 更多