Logo
Section 1 / 3

创建播放列表并添加项目到播放列表

学习如何使用 Spotify 用户账户创建播放列表并添加项目。

导入

创建播放列表并添加项目到播放列表

学习如何使用 Spotify 用户账户创建播放列表并添加项目。

请求体参数类别类型描述
name必需字符串我们希望分配给播放列表的名称。
public可选布尔值设置播放列表为公开或私有。可选值为 true 和 false。此参数的默认值为 true。
collaborative可选布尔值设置播放列表是否为协作模式。如果播放列表是协作的,你可以邀请其他用户编辑你的播放列表。其默认值为 false。若要设为 true,则 public 参数的值必须为 false。
description可选字符串播放列表的描述
1const endpointUrl = new URL('https://api.spotify.com/v1/me'); 2 3headerParameters = { 4 'Content-Type': 'application/json', 5 'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}' 6} 7 8const options = { 9 method: 'GET', 10 headers: headerParameters, 11}; 12 13async function fetchUserID() { 14 try { 15 const response = await fetch(endpointUrl, options); 16 printResponse(response); 17 } catch (error) { 18 printError(error); 19 } 20} 21 22fetchUserID();
1const endpointUrl = new URL('https://api.spotify.com/v1/users/{{USER_ID}}/playlists'); 2 3headerParameters = { 4 'Content-Type': 'application/json', 5 'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}' 6} 7 8bodyParameters = { 9 name: 'My playlist', 10 description: '使用 Spotify API 创建的播放列表' 11} 12 13const options = { 14 method: 'POST', 15 headers: headerParameters, 16 body: JSON.stringify(bodyParameters), 17}; 18 19async function createPlaylist() { 20 try { 21 const response = await fetch(endpointUrl, options); 22 printResponse(response); 23 } catch (error) { 24 printError(error); 25 } 26} 27 28createPlaylist();

响应字段类型描述
description字符串我们提供的播放列表描述。
owner对象数组包含用户信息。
tracks对象数组包含播放列表中歌曲的信息。新创建的播放列表中该字段为空。
images对象数组包含播放列表封面图片的链接及其尺寸信息。目前该字段为空。
id字符串播放列表的 ID。
name字符串播放列表的名称。

查询参数类别类型描述
position可选整数指定要添加的项目在播放列表中的位置。

参数类别类型描述
position可选整数指定要添加的项目在播放列表中的位置。
uris可选数组包含要添加项目的 Spotify URIs。这些 URIs 可通过 Get Track、Get Episodes 或 Search for Item 接口获取。数组最多可包含 100 个 URIs。如果查询参数中已包含 URIs,则此处的 URIs 将被忽略。
1const endpointUrl = new URL('https://api.spotify.com/v1/playlists/{{PLAYLIST_ID}}/tracks'); 2 3const queryParameters = new URLSearchParams({ 4 uris: 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh,spotify:track:1301WleyT98MSxVHPZCA6M' 5}); 6 7headerParameters = { 8 'Content-Type': 'application/json', 9 'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}' 10} 11 12bodyParameters = {} 13 14const options = { 15 method: 'POST', 16 headers: headerParameters, 17 body: JSON.stringify(bodyParameters), 18}; 19 20async function addTracks() { 21 try { 22 endpointUrl.search = queryParameters; 23 const response = await fetch(endpointUrl, options); 24 printResponse(response); 25 } catch (error) { 26 printError(error); 27 } 28} 29 30addTracks();

专辑

用户播放列表及播放列表信息