node.js - Spotify:创建播放列表
问题描述
扩展https://github.com/spotify/web-api-auth-examples - authorization_code 文件夹中的代码。
它可以注销 access_token ,但随后在发布请求时挂起:
app.get('/createPlaylist', function(req, res) {
console.log('access_token=' + access_token)
request.post(
'https://api.spotify.com/v1/users/' + user_id + '/playlists',
{
headers: {
'Authorization' : access_token,
'Content-Type': 'application/json'
},
json: {
"collaborative": false,
"description": null,
// "external_urls": {
// "spotify": "http://open.spotify.com/user/thelinmichael/playlist/7d2D2S200NyUE5KYs80PwO"
// },
// "followers": {
// "href": null,
// "total": 0
// },
// "href": "https://api.spotify.com/v1/users/thelinmichael/playlists/7d2D2S200NyUE5KYs80PwO",
// "id": "7d2D2S200NyUE5KYs80PwO",
"href": null,
"id": null,
"images": [],
"name": "A Generated Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/1150816110"
},
"href": "https://api.spotify.com/v1/users/1150816110",
"id": "1150816110",
"type": "user",
"uri": "spotify:user:1150816110"
},
"public": true,
// "snapshot_id": "s0o3TSuYnRLl2jch+oA4OEbKwq/fNxhGBkSPnvhZdmWjNV0q3uCAWuGIhEx8SHIx",
"tracks": {
"href": "https://api.spotify.com/v1/users/thelinmichael/playlists/7d2D2S200NyUE5KYs80PwO/tracks",
// "href": "https://api.spotify.com/v1/users/kb8mc65qdvz4lz0gdk0i4ztp3/playlist/46RFjEgbskxglR8rVsu38x/tracks",
"items": [],
"limit": 100,
"next": null,
"offset": 0,
"previous": null,
"total": 0
},
"type": "playlist",
// "uri": "spotify:user:thelinmichael:playlist:7d2D2S200NyUE5KYs80PwO"
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
});
知道有什么问题吗?
'看起来你的帖子主要是代码; 请添加更多细节。
StackOverflow 有哪些细节?我只想知道为什么 POST 请求挂起。
将我的代码修改为 Kevin 的规范......不敢相信我误读了输入的输出......但它仍然挂起......节点版本 v12.14.1
app.get('/createPlaylist', function (req, res) {
console.log('access_token=' + access_token)
request.post(
'https://api.spotify.com/v1/users/' + user_id + '/playlists',
{
headers: {
'Authorization': access_token,
'Content-Type': 'application/json'
},
json: {
name: 'A generated playlist',
public: true,
collaborative: false,
description: 'Generated at ' + Date.now(),
}
},
function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
}
);
});
解决方案
将您的代码与文档进行比较,看起来代码将 JSON 输出作为示例中的输入发送。
要创建一个播放列表,只有 4 个 body 参数:
name
- 字符串 - 必填public
- 布尔值 - 可选collaborative
- 布尔值 - 可选description
- 字符串 - 可选。
我不知道所有其他信息来自哪里,但 API 可能会挂断所有这些信息。还要确保您具有为用户创建播放列表的权限所涉及的正确范围。
该请求看起来会被削减:
request.post(
'https://api.spotify.com/v1/users/' + user_id + '/playlists',
{
headers: {
'Authorization' : access_token,
'Content-Type': 'application/json'
},
json: {
name: 'A generated playlist',
public: true,
description: 'A playlist generated by JS'
}
}
...
推荐阅读
- angular - D3堆栈区域问题在角度使用打字稿
- scala - Scala 对象构造函数
- azure-resource-manager - 在 Azure 服务总线订阅 SqlFilter 表达式中使用参数
- python - 迭代加法的 Python 语法错误,例如 x += 1
- r - 如何使用上传文件中的 DT 和 Shiny 编辑表格?
- angular - Angular2 patchValue JSON数据到formArray
- android - 添加firebase和编译的问题
- google-analytics - AMP 分析 - 链接跟踪,跟踪点击了哪些链接/数据属性
- python - 没有for循环的numpy数组
- squish - 什么 Squish 模块包含“nativeType”低级函数?