0
votes

I am trying to give users who are connected with Soundcloud the ability to create new playlists ( sets ) stored on Soundcloud with their SC account. Very basic. As a test case the example I am testing is almost exactly taken from the Soundcloud dev docs except I have added a different sound id.

<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
// initialize client with app credentials
SC.initialize({
  client_id: 'MY_CLIENT_ID',
  redirect_uri: 'http://localhost.local/~****/sc/callback.html'
});

// initiate auth popup and create new playlist
SC.connect(function() {
    SC.get('/me', function(me) {
        console.log(me.username);
    });

    var tracks = [12573606].map(function(id) { return { id: id }; });
    SC.post('/playlists', {
        playlist: { title: 'My Playlist', tracks: tracks }
    });
});

The copy from the SC.post and its response are as follows:

POST https://api.soundcloud.com/playlists 422 (Unknown Error) sdk.js:1
window.SC.SC.Helper.merge._xhrRequest sdk.js:1
window.SC.SC.Helper.merge._request sdk.js:1
window.SC.SC.Helper.merge._apiRequest sdk.js:1
window.SC.SC.Helper.merge.post sdk.js:1

(anonymous function) localhost.local:21
(anonymous function) sdk.js:1
f.onreadystatechange sdk.js:1

XHR finished loading: "https://api.soundcloud.com/playlists". sdk.js:1
window.SC.SC.Helper.merge._xhrRequest sdk.js:1
window.SC.SC.Helper.merge._request sdk.js:1
window.SC.SC.Helper.merge._apiRequest sdk.js:1
window.SC.SC.Helper.merge.post sdk.js:1
(anonymous function) localhost.local:21
(anonymous function) sdk.js:1
f.onreadystatechange sdk.js:1

Uncaught TypeError: object is not a function sdk.js:1
(anonymous function) sdk.js:1
f.onreadystatechange

I've tried a couple different solutions as well as creating the url request myself and posting along with it my client_id but haven't had any success.

I've noticed an inconsistency in the dev docs too. I've noticed is that in the dev docs there is a create playlist JS SDK example however in the APIGEE ( API ) Console there isn't reference to a post /playlists request, just /playlists for a get... Does this not even exist in the current Soundcloud api than?

The other thing that crossed my mind was that since it was a 422 error can you only add tracks to playlists when a user is uploading the track originally with the sdk?

Has anyone else run into this issue? Has anyone found any solutions to it?

Thanks,

1

1 Answers

2
votes

I am using the SoundCloud API via Cocoa/Objective C and had the same problem of getting Error 422 when trying to update playlists. My solution was in understanding that the parameter value must be an array of track "id" strings, and that the parameter key is "playlist[tracks][][id]".

So, for Objective C, the code is:

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
NSString *trackList = @"60832750,61311756,60832782"; // for example
NSArray *tracks = [self.trackList componentsSeparatedByString:@","];
[parameters setObject:tracks forKey:@"playlist[tracks][][id]"];

Of course, your exact code will depend on the language you're using. For me, the trick was getting that key with all its brackets correct.