我正试图掌握Spotify API,但我不知道如何播放播放列表或专辑。请参阅下面的代码:
// Called when the Spotify Web Playback SDK is ready to use
window.onSpotifyWebPlaybackSDKReady = () => {
// Define the Spotify Connect device, getOAuthToken has an actual token
// hardcoded for the sake of simplicity
var player = new Spotify.Player({
name: "A Spotify Web SDK Player",
getOAuthToken: (callback) => {
callback(
"<Token to be placed here>"
);
},
volume: 0.3,
});
// Called when connected to the player created beforehand successfully
player.addListener("ready", ({ device_id }) => {
console.log("Ready with Device ID", device_id);
const play = ({
context_uri,
playerInstance: {
_options: { getOAuthToken, id },
},
}) => {
getOAuthToken((access_token) => {
fetch(
`https://api.spotify.com/v1/me/player/play?device_id=${device_id}`,
{
method: "PUT",
body: JSON.stringify({ uris: [context_uri] }),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${access_token}`,
},
}
);
});
};
play({
playerInstance: player,
context_uri: "spotify:playlist:37i9dQZF1EIZKYrELNKNNT",
position_ms: 0,
});
});
// Connect to the player created beforehand, this is equivalent to
// creating a new device which will be visible for Spotify Connect
player.connect();
console.log("playing?");
};
实际问题是这一部分:context_uri: "spotify:playlist:37i9dQZF1EIZKYrELNKNNT
这将返回400错误:
{
"error" : {
"status" : 400,
"message" : "Unsupported uri kind: playlist_v2"
}
}如果我用特定的声道uri替换它,比如spotify:track:72boGlgSwUK01n44O2tOCv,一切都很好。
如何传递播放列表uri而不是特定的曲目?
发布于 2022-11-19 11:21:12
我不知道这是否对你有帮助,但我也犯了类似的错误,就像这样对我有用:
data: JSON.stringify({ context_uri: context }),其中"context“是AJAX方法的局部变量。
所以基本上是这样的:
data: JSON.stringify({ context_uri: "spotify:playlist:6SET13ADRq431RxMsMVlo4" }),https://stackoverflow.com/questions/68047533
复制相似问题