如何从 Spotify Search API 获取数据并将其解析为 JSON? [在 node.js 中

Posted

技术标签:

【中文标题】如何从 Spotify Search API 获取数据并将其解析为 JSON? [在 node.js 中【英文标题】:How to fetch data from Spotify Search API and parse it to JSON? [In node.js 【发布时间】:2021-08-08 08:35:24 【问题描述】:

这是我的 app.js:

const express = require("express");
//const https = require("https");
const app = express();

app.get("/", function(req, res) 
  
  const query = niall;

  // Follow procedure here to get access_token and refresh_token: https://benwiz.com/blog/create-spotify-refresh-token/

  const access_token = access_token;
  const token = "Bearer " + access_token;
  var searchUrl = "https://api.spotify.com/v1/search?q=" + query + "&type=track&limit=4";





  ////////WRITE YOUR CODE HERE//////////




  
);
// Starting the server. Should this be placed at the top of all other commands?
app.listen(3000, function() 
  console.log("Server is running on port 3000.")
)

预期行为:

我们将使用 https.get 方法从 api 端点获取结果。

返回的数据将被解析为JSON。

我们需要将此 JSON 输出中的轨道 ID 的值存储到一个变量中,并将其记录在控制台中。

有用的资源:

按照此处的步骤获取 access_token:https://benwiz.com/blog/create-spotify-refresh-token/ 在这里了解参数:https://developer.spotify.com/documentation/web-api/reference/#category-search Spotify Web Console 了解 JSON 排列:https://developer.spotify.com/console/get-search-item/

我真的需要帮助。任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

////////WRITE YOUR CODE HERE////////// 替换为

axios.get(searchUrl, 
      headers: 
        'Authorization': token,
      
    )
    .then((resAxios) => 
      console.log(resAxios.data)
      spotifyResult = resAxios.data;

      //Extracting required data from 'result'. The following "items[0].id.videoId" is the address of the data that we need from the JSON 'ytResult'.
      let spotifyTrackIdAppJs00 = spotifyResult.tracks.items[0].id;
      let spotifyAlbumIdAppJs00 = spotifyResult.tracks.items[0].album.id;
      let spotifyArtistIdAppJs00 = spotifyResult.tracks.items[0].artists[0].id;
      console.log("Fetched values: " + spotifyTrackIdAppJs00 + ", " + spotifyAlbumIdAppJs00 + ", " + spotifyArtistIdAppJs00);

      // The 'results' named EJS file is rendered and fed in response. The 'required' data is passed into it using the following letiable(s).
      // A folder named 'views' has to be in the same directory as "app.js". That folder contains 'results.ejs'.
      res.render("results", 
        spotifyTrackIdEjs00: spotifyTrackIdAppJs00,
        spotifyAlbumIdEjs00: spotifyAlbumIdAppJs00,
        spotifyArtistIdEjs00: spotifyArtistIdAppJs00
      );
      console.log("Values to be used in rendered file: " + spotifyTrackIdAppJs00 + ", " + spotifyAlbumIdAppJs00 + ", " + spotifyArtistIdAppJs00);
    )
    .catch((error) => 
      console.error(error)
    )

我还需要专辑和艺术家的 ID 值。请相应修改。

【讨论】:

以上是关于如何从 Spotify Search API 获取数据并将其解析为 JSON? [在 node.js 中的主要内容,如果未能解决你的问题,请参考以下文章

使用 ISRC 代码从 Spotify API 获取多个曲目

如何使用 spotify api 获取艺术家的所有曲目?

从 Spotify End Point 获取所需的图像

如何在我当前使用 Spotify Search API 的代码上应用 Spotify API 身份验证?

如何从 Spotify 播放列表中获取数据

如何从 Spotify API 获取曲目 URI