How can I do a /resolve with the Soundcloud Javasc

2020-06-29 09:08发布

问题:

I would like a user of my app to

  1. paste in a Soundcloud track URL to a <input> field
  2. press a submit <button>
  3. have a function return the track's id

So far the only way I know how to do a /resolve is via command line's curl command as demonstrated here http://developers.soundcloud.com/docs/api/reference#resolve. Is there a way to do this via a Javascript function?

e.g.

SC.resolve(trackURL, id, function(id, error){});

回答1:

All curl does is an HTTP request. JavaScript is definitely able to do an HTTP request, most common way of doing that is by using XMLHttpRequest also known as AJAX.

Popular libraries, such as jQuery simplify issuing these requests, for example:

// replace YOUR_CLIENT_ID with one you can get from http://soundcloud.com/you/apps
var trackUrl = 'http://soundcloud.com/matas/hobnotropic';
$.get(
  'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=YOUR_CLIENT_ID', 
  function (result) {
    console.log(result);
  }
);

Working example here.



回答2:

You can actually use SC.get() directly for almost every resources in the API (without jQuery). Like this:

<script src="http://connect.soundcloud.com/sdk.js"></script>

<script>

   SC.initialize({
       client_id: "YOUR_CLIENT_ID"
   });

   SC.get("/resolve/?url=https://soundcloud.com/dogwill", {limit: 1}, function(result){
       console.log(result);
   });

</script>