如何解决如何修复,“{ error: { status: 400, message: 'Bad search type field tracking' } }”在使用 Spotify Search API 时发生
-
后端:node.js,express。使用的其他模块:body-parser、request。
-
我正在尝试从 Spotify Search API 获取曲目 ID。
-
用户发出 POST 请求。请求被馈送到服务器。
-
服务器向端点发出 GET 请求。
-
端点返回数据。
-
数据被解析为 JSON。
问题:解析时,终端记录以下错误。
{ error: { status: 400,message: 'Bad search type field tracks' } }
要关注的代码:
request({
url: searchUrl,headers: {
"Authorization": token
}
},function(err,res) {
if (res) {
var data = JSON.parse(res.body);
// var spotifyTrackIdAppJs00 = data.tracks.items[0].id;
console.log(data);
// console.log(trackId);
}
)
完整的 app.js 代码:
// It is a nodejs,expressjs project.
const express = require("express");
// 'body-parser' is used to access tags from the html file. We'll be using it to access queryValue.
const bodyParser = require("body-parser");
// request package; to fetch data from search endpoint.
const request = require("request");
// This app constant is created to be able to access the menthods available in 'express' package.
const app = express();
// 'urlencoded' helps access html data. Other data formats Could JSON etc.
// body-parser required as to exclusively define "extended: true" although this is no use to us.
app.use(bodyParser.urlencoded({
extended: true
}));
// This sets a static directory to look for source files like css,js,img. These file links are mentioned in html or ejs files.
// A folder named 'public' has to be in the same directory as "app.js". The source files are stored here.
app.use(express.static("public"));
// ejs view engine has been used to use app.js variables into the output ejs file.
app.set('view engine','ejs');
// Variable(s) to store the data fetched from API endpoint.
var data = "";
// The page to load when the browser (client) makes request to GET something from the server on "/",i.e.,from the homepage.
// This GET request is made as soon as the homepage url is entered in the address bar od browser,automatically.
app.get("/",function(req,res) {
res.sendFile(__dirname + "/index.html");
});
// The data that server should POST when the POST request is sent by the client,upon entering the search queryValue,in the search bar (form).
app.post("/",res) {
// The user input query. We are using body-parser package here.
const query = req.body.queryValue;
// 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=tracks&limit=4";
request({
url: searchUrl,res) {
if (res) {
var data = JSON.parse(res.body);
// var spotifyTrackIdAppJs00 = data.tracks.items[0].id;
console.log(data);
// console.log(trackId);
}
// res.render("results",{
// spotifyTrackIdEjs00: spotifyTrackIdAppJs00
// });
// console.log("Value to be used in rendered file: " + spotifyTrackIdAppJs00);
})
});
// 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.")
});
有用的资源:
- 按照此处的步骤获取 access_token:https://benwiz.com/blog/create-spotify-refresh-token/
- 在此处了解参数:https://developer.spotify.com/documentation/web-api/reference/#category-search
- 了解 JSON 排列的 Spotify Web 控制台:https://developer.spotify.com/console/get-search-item/
我绝对是初学者。请像我五岁一样教我。我将不胜感激。
解决方法
你的代码看起来不错,但你有一个错别字,把一切都搞砸了。这个:
var searchUrl = "https://api.spotify.com/v1/search?q=" + query + "&type=tracks&limit=4";
Spotify's API 的参数 type 具有以下有效值: 专辑、艺术家、播放列表、曲目、节目和剧集。
在您的 searchUrl 上,您使用的是 tracks(复数)而不是 track,所以应该是:
var searchUrl = "https://api.spotify.com/v1/search?q=" + query + "&type=track&limit=4";
对于未来:HTTP 的 400 代码意味着“错误请求”,这意味着服务器告诉您请求发送了错误的内容。当您收到这种错误响应时,通常最好仔细检查您正在使用的 API 所需的参数/主体及其接受的值
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。