我有一个带有React组件的ExpressJs服务器。服务器应该处理来自外部的请求,一个请求应该在当前不播放时播放Spotify API中的一首歌。
app.post("/play", (req, res) => {
try {
// requesting to play uses query params
id = req.query.id;
currPlayingID = 0;
// get the currently playing song from the SPotify API
axios({
url: "https://api.spotify.com/v1/me/player/currently-playing",
method: "get",
headers: {
authorization: `Bearer ${access_token}`,
},
})
// set the currently Playing ID or to zero if nothing is playing
.then((response) => {
if (response.data !== null) {
currPlayingID = response.data.id;
} else {
currPlayingID = 0;
}
});
// only play the song if its not currently playing
if (id !== currPlayingID) {
// making a axios request to the Spotify API to play the Song with the ID
axios({
url: "https://api.spotify.com/v1/me/player/play/",
method: "put",
headers: {
authorization: `Bearer ${access_token}`,
},
data: {
uris: [`spotify:track:${id}`],
},
});
res.status(204);
}
} catch (error) {
res
.status(404)
.json({ message: "Couldn't get Info from Spotify API", error: error });
}
});
问题:
当我在设备本身上启动服务器时,代码工作(所以在我的桌面PC上的本地服务器),但当我在我的RaspberryPI上启动服务器时,我无法处理到此端点的请求/播放.是的,我更新了所有的IP地址,无处不在.
但更有趣的部分是使用React客户端,我收到以下错误:
Failed to load resource: net::ERR_CONNECTION_REFUSED
向POSTMAN请求,我得到以下信息:
Mixed Content Error: The request has been blocked because it requested an insecure HTTP resource
在服务器端,我使用python脚本从一个请求中获得:
[UnhandledPromiseRejection: 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(). The promise rejected with the reason "AxiosError: Request failed with status code 400".] {
code: 'ERR_UNHANDLED_REJECTION'
}
我不知道如何修复每个错误,如果它是一个修复。基本上我发现这是一个问题,拒绝请求从外部localhost,因为与cURL在我的ssh终端上它的工作。
2条答案
按热度按时间i2byvkas1#
我正在学习express,所以我不是Maven,但是我正在查看您的错误。我建议您尝试asyncHandler module。它处理异步请求和异常。
jgwigjjp2#
我遇到过类似的问题,因为当我通过Axios发送API请求时,我的令牌为null/空/错误,因此请确保您的令牌正确
这是我要求格式