【发布时间】:2020-07-06 11:58:56
【问题描述】:
我试图在 node.js 中使用 axios 向 spotify API 发出获取请求。但是,我总是收到一个 400 错误的请求。有人可以帮我吗?代码sn-p和错误贴在下面。
app.get('/api/search', async (req, res) => {
const spotify_search_one = await axios.get('https://api.spotify.com/v1/search', {
headers: {
'Authorization': keys.spotifyClientId
},
params: {
q: "face",
type: "track"
}
});
console.log(spotify_search_one);
})
报错如下
UnhandledPromiseRejectionWarning: Error: Request failed with status code 400
[0] at createError (/Users/uddhavbhagat/Desktop/Projects/TuneIn/node_modules/axios/lib/core/createError.js:16:15)
[0] at settle (/Users/uddhavbhagat/Desktop/Projects/TuneIn/node_modules/axios/lib/core/settle.js:17:12)
[0] at IncomingMessage.handleStreamEnd (/Users/uddhavbhagat/Desktop/Projects/TuneIn/node_modules/axios/lib/adapters/http.js:236:11)
[0] at IncomingMessage.emit (events.js:322:22)
[0] at endReadableNT (_stream_readable.js:1187:12)
[0] at processTicksAndRejections (internal/process/task_queues.js:84:21)
[0] (node:12697) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
【问题讨论】:
-
试试
'Authorization': 'Bearer '+keys.spotifyClientId -
这似乎不起作用。我试过了
-
错误变为401错误。不知道怎么解决这个问题
-
spotifyClientId 听起来不对,它应该是一个有效的访问令牌。请参阅文档:developer.spotify.com/documentation/general/guides/…
-
访问令牌是否与clientID不同? ClientID 本质上就是我从开发者控制台制作的 API Key
标签: javascript node.js api spotify