Reputation: 63
I have a problem with my privates tracks. I have public track and privates tracks in my soundcloud account. When a track is public is correcte, but when i'm a private track the api return an 404 error : GET http://api.soundcloud.com/tracks/123?client_id=xxxxx&format=json&_status_code_map[302]=200 404 (Not Found) in my console.
Here my function for to play a track :
// Play a Single track
$('#app').on('click','.play_btn_single',function(event){
event.preventDefault();
var el = $(this);
var id = el.attr('id');
SC.stream('/tracks/'+id,function(sound){
sound.play();
});
});
Thank you of your help.
Upvotes: 4
Views: 4448
Reputation: 13906
It is possible to play a private track. The easiest is to use private HTML5 widget (as Jameal G. correctly noticed) – for that you just need to click "share" button on a track’s page, select "embed" tab and copy the iframe code to insert on your website.
Otherwise, if you want to play it with your own code, then you have to use secret_token
that you can get when sharing the track on the website (when authorised) or when querying the API, also authorised (so providing the HTTP header Authorization: Oauth 123…
).
Then the client application can use that secret token to query API and play your sound without authorising the users. Below is a simple example where I share my private track:
HTML:
<audio controls></audio>
JS, assuming jQuery, but the idea should be clear:
var clientId = 'client_id=YOUR_CLIENT_ID',
sekretToken = 'secret_token=s-wwC6c',
trackUri = 'https://api.soundcloud.com/tracks/128310939.json?' +
secretToken + '&' + clientId,
audio = jQuery('audio');
jQuery
.get(trackUri)
.then(function (result) {
// do not forget to supply client_id also when querying for stream url
audio.attr('src', result.stream_url + clientId);
});
http://jsbin.com/yerilu/edit?html,js,output
UPD. How to find the secret token.
On SoundCloud website, go to the page of the private track that you own, while authorised on the website, then press "share" button (second from the left under "write a comment" input). You will see a text input field titled "Private Share" – copy the link and the last part after the slash is the secret token.
You can also query the API when authorised (so you must send Oauth HTTP header, like Authorization: Oauth 123…
, of course you will have a longer number for your own token) for your tracks and the track representation will have secret_token
property with the value of the token you could use.
Upvotes: 2
Reputation: 434
You need to provide an authentication token if you wish to play sounds from an account that are not public. From SoundCloud's API Guide:
"Note that as long as the sound is public, you'll only need to provide a client_id when creating a client. If you would like to access the stream URL for a private sound, you'll need to authenticate your app."
For instructions on authentication, see: https://developers.soundcloud.com/docs/api/guide#authentication
Upvotes: 2