【问题标题】:"Create a Playlist" with Spotify Web API returning 201 but without a playlist object使用 Spotify Web API “创建播放列表”返回 201 但没有播放列表对象
【发布时间】:2019-11-03 12:49:21
【问题描述】:

我正在尝试使用 Spotify Web API 创建一个网络应用程序,其中一部分包括创建一个播放列表,然后用一些曲目填充它。

根据API reference,对请求的响应应该返回一个包含播放列表详细信息的播放列表对象,对我来说更重要的是,新播放列表的 API 端点,以便我可以立即使用它来添加曲目。

我得到的响应是 201,我可以看到在 Spotify 中创建的新播放列表,但响应不包含正文,也没有与创建的播放列表交互的端点。

是我的请求格式不正确还是我在响应中遗漏了什么?

// Test create playlist
  const createPlaylist = async (accessToken, userID, tracks) => {
    // Create empty playlist and retrieve endpoint
    const emptyPlaylist = await fetch(`https://api.spotify.com/v1/users/${userID}/playlists`, {
      method: 'POST',
      body: JSON.stringify({
        'name': 'Intersection Test',
        'public': false,
      }),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + accessToken
      }
    })
    .then(async response => {
      // Add tracks to playlist
      if (tracks.length > 100) error("Playlist too large for one call");
      const fillPlaylist = await fetch(response.url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + accessToken
        },
        body: {
          'uris': tracks
        }
      });
    });

Image of response in Chrome console here

【问题讨论】:

  • 文档表明响应中有一个标头,指示新播放列表的 url。您可以在响应中检查该 url 的标题吗?
  • 有标题位置字段吗?那么答案就是在那里找到
  • 是的,这也是我预期的,但标题完全是空的。
  • 您的回复图片,具体来自哪里?它看起来有一个 url 标头条目。
  • 该 URL 与发送请求的 URL 相同,没有提供任何有关播放列表的标识。我要查找的网址应采用“api.spotify.com/v1/playlists/{playlist_id}/tracks”格式

标签: javascript http async-await spotify fetch-api


【解决方案1】:

为了将来参考,我的问题是我在使用正文之前忘记应用 response.json(),所以我打印的是响应流而不是 Promise 的实际分辨率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多