Soundcloud API Error NS_ERROR_DOM_BAD_URI: Access

2019-04-18 01:45发布

问题:

I am just getting myself acquainted with Soundcloud's API and I am having some trouble. As far as I can tell, all I need in SC.initialize is a client_id. I have used the tutorials at Code Academy to get started and it was great. Now that I am actually trying to implement something I am running into some trouble.
When I ran my code in Code Academy, it did exactly what I wanted it to do. Now that I am trying to run it in a browser, I am getting a blank screen and this error:

NS_ERROR_DOM_BAD_URI: Access to restricted URI denied @ http://connect.soundcloud.com/sdk

After doing some research, I have found that those errors are related to domain prefixes. So I tried changing it to http://www.connect.soundcloud.com/sdk. But when I do that, I get a different error:

SC is not defined

AHHHH What am I doing wrong?!
I am new to using API's, and any help at all would be very greatly appreciated.
Here is what I am doing:
(JavaScript)

SC.initialize({
    client_id: 'hidden for privacy',
});

$(document).ready(function() {
    SC.get('/users/5577686/tracks', {limit:7}, function(tracks) {
        $(tracks).each(function(index, track) {
            $('#tracktitle').append($('<li></li>').html(track.title));
            $('#trackimage').append("<img src='" + track.artwork_url + "' />");
            $('#play').append("<a href='" + track.permalink_url + "' >" + "Play" + "</a>");
        });
    });
});

(HTML)

<!DOCTYPE HTML>
<html>
    <head>
        <script src="http://connect.soundcloud.com/sdk.js"></script>
        <script src="soundcloud.js"></script>
    </head>
    <body>
        <div id="tracktitle"></div>
        <div id="trackimage"></div>
        <div id="play"></div>
    </body>
</html>

I don't really think there is anything wrong with the code, as everything seemed to be working fine in Code Academy. I think it is more of an issue with familiarizing myself with the API. Do I need to do some further authentication? Do I need something more than just the client ID? Again I am very stuck and would appreciate any amount of help on this. Thanks for reading.

(I also followed along with Coding for GOOD's Soundcloud API Integration tutorial step-by-step and I am getting the same exact errors, so this further confirms that the code is probably not the problem, but connecting to the API may be)

回答1:

The problem here is that when connecting to an API which is being hosted on another server, you must be using the same protocol. For local files, you use the file protocol (file://), wheres soundcloud uses: http:// or https://

Follow these steps:

  1. Get a server (XAMPP/LAMPP/WAMPP for PHP/Regular HTML, NodeJS for JS server, or Tornado for Python)
  2. Find the protocol which your server uses

If you server uses the http protocol, then your domain must be http://soundcloud.com/..., but if your server uses the https protocol, then the domain for the API must be https://..... So once you get the protocols to match, then you will be able to pass data through the API.