Add track to playlist SoundCloud API

浪尽此生 提交于 2019-12-12 04:25:23

问题


In my Windows Phone App, I'm using the following code to add a track to playlist (i.e. a PUT request to playlists/id endpoint)

    using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AccessToken);
                HttpResponseMessage response = await httpClient.PutAsync(endpoint, new StringContent(data));
                response.EnsureSuccessStatusCode();
            }

where "data" is JSON data the form:

    {"playlist":{"tracks":["TrackId(to be added)"]}}

The above code returns "OK"(200) response but the track is NOT added to the playlist!

What am I doing wrong? Stuck on it for two days. Thanks in advance!


回答1:


I use Put to replace track ids in set. here is sample code

for (String s : trackIds)
    nameValuePairs.add(new BasicNameValuePair("playlist[tracks][][id]", s.trim()));

String url = "https://api.soundcloud.com/playlists/" + setId + ".json";
httpPut(url, nameValuePairs);



回答2:


The problem was that the JSON data (of the request body) was not formatted correctly. "data" must be of the form:

    {"playlist":{"tracks":[{"id":"__"}, {"id":"__"}, {"id":"__"}]}}

Here the id-value pair must be present for

  • every track already present in the playlist, as well as

  • the track that you want to add to the playlist

(Remember, this is a PUT request. So, you need to update data i.e. update the "tracks" property of the "playlist")



来源:https://stackoverflow.com/questions/28834713/add-track-to-playlist-soundcloud-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!