根据下面的文档共享链接,
https://developer.apple.com/documentation/applemusicapi/create_a_new_library_playlist
我能够获得开发人员令牌和音乐用户令牌,但我正在获得"无效请求体"(400个错误请求)的错误。
这是我用来创建苹果音乐播放列表的函数。
func createAppleMusicPlaylist() {
let playlistURL = URL(string: Config.appleCreatePlaylist)!
var playlistRequest = URLRequest(url: playlistURL)
playlistRequest.httpMethod = "POST"
let params = ["name" : kPlaylistName,
"description" : kPlaylistDesc]
playlistRequest.httpBody = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
playlistRequest.addValue("Bearer (DeveloperToken)", forHTTPHeaderField: "Authorization")
playlistRequest.addValue("(UserToken)", forHTTPHeaderField: "Music-User-Token")
URLSession.shared.dataTask(with: playlistRequest) { data, response, error in
guard error == nil else {
print("(String(describing: error?.localizedDescription))")
return
}
if let data = data {
let json = try? JSONDecoder().decode(AppleCreatePlaylistModel.self, from: data)
}
}.resume()
}
API的回应:
{
"errors": [
{
"id": "X4IK3UU6LYGGRSQ6U2KQANL63A",
"title": "Invalid Request Body",
"detail": "Unable to parse request body",
"status": "400",
"code": "40007"
}
]
}
请建议所需的主体参数。谢谢!
您需要遵循API文档,创建一个新的库播放列表,并遵循在您的情况下导致问题的HTTP Body部分。
HTTP的身体LibraryPlaylistCreationRequestPOST请求,包含要添加的播放列表的名称、曲目和父播放列表文件夹。
现在,让我们检查LibraryPlaylistCreationRequest
:
属性
- 属性
LibraryPlaylistCreationRequest。Attributes
(必选)
包含名称和字符串的字典
LibraryPlaylistCreationRequest。Relationships
可选键包括新播放列表的曲目
所以,body应该是这样的,在这一点上:
{
"attributes": ..., //Required
"relationships": ... //Optional
}
让我们看看LibraryPlaylistCreationRequest.Attributes
:
所以,现在,JSON应该看起来像这样(我跳过了属性描述
- <
string
播放列表的描述- 名称/strong>
string
(Required)播放列表的名称。description
string
relationships
部分,因为你不使用它):
{
"attributes": {
"name": "playlist name",
"description": "playlist description"
}
}