其次,展示目前歌唱的插图。
Context: using app Router
since, to get token, spotify requires me to make a serverside call, Hence im making the complete request to fetch song from a api endpoint localhost/api/player
const getAccessToken = async () => {
const client_id = key ; // Replace with your client ID
const client_secret = key ; // Replace with your client secret
fetch( https://accounts.spotify.com/api/token , {
method: POST ,
headers: {
Content-Type : application/x-www-form-urlencoded ,
Authorization: `Basic ${btoa(`${client_id}:${client_secret}`)}`,
},
body: grant_type=client_credentials
})
.then(response => response.json())
.then(data => {
console.log(data.access_token);
return data.access_token;
});
return null;
}
export const fetchCurrentPlayingSong = async () => {
const accessToken = await getAccessToken();
const response = await fetch( https://api.spotify.com/v1/me/player/currently-playing , {
headers: {
Authorization : `Bearer ${accessToken}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch currently playing song: ${response.statusText}`);
}
const song = await response.json();
return song;
};
差错:
无处理
rror: 违约出口不是第2页中的一种反应部分:“/api/player”
i tried to run this on clientside, but spotify doesnt allow fetching tokens on client side. i just want to get serversideprops from sever about the currently playing song every 5 seconds or the best way to display currently playing song