首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spotify Web API -如何播放播放列表

Spotify Web API -如何播放播放列表
EN

Stack Overflow用户
提问于 2021-06-19 14:05:30
回答 1查看 367关注 0票数 1

我正试图掌握Spotify API,但我不知道如何播放播放列表或专辑。请参阅下面的代码:

代码语言:javascript
运行
复制
// 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错误:

代码语言:javascript
运行
复制
{
  "error" : {
    "status" : 400,
    "message" : "Unsupported uri kind: playlist_v2"
  }
}

如果我用特定的声道uri替换它,比如spotify:track:72boGlgSwUK01n44O2tOCv,一切都很好。

如何传递播放列表uri而不是特定的曲目?

EN

Stack Overflow用户

发布于 2022-11-19 11:21:12

我不知道这是否对你有帮助,但我也犯了类似的错误,就像这样对我有用:

代码语言:javascript
运行
复制
data: JSON.stringify({ context_uri: context }),

其中"context“是AJAX方法的局部变量。

所以基本上是这样的:

代码语言:javascript
运行
复制
data: JSON.stringify({ context_uri: "spotify:playlist:6SET13ADRq431RxMsMVlo4" }),
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68047533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档