使用soundloud API从用户Feed中获取所有曲目

时间:2015-10-18 21:40:16

标签: javascript api soundcloud

目前,我尝试使用javascript soundcloud API包装器来获取用户Feed中的所有声音,如下所示:

$('head').append("<script src='https://connect.soundcloud.com/sdk/sdk-3.0.0.js'>");

SC.initialize({
    client_id: 'YOUR_CLIENT_ID',
    redirect_uri: '/callback.html'
});

// initiate auth popup
SC.connect(function() {
    SC.get('/me', function(me) {
        alert('Hello, ' + me.username);
    });
});

var dashTracks = SC.get('/me/activities/tracks/affiliated');

但是当我查看控制台时,出现错误

Uncaught ReferenceError: SC is not defined

我不太确定如何绕过它。

1 个答案:

答案 0 :(得分:1)

在执行代码之前等待加载脚本。

var s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'https://connect.soundcloud.com/sdk/sdk-3.0.0.js';
$('head').append(s);

s.onload = init;

function init() {
    SC.initialize({
        client_id: 'YOUR_CLIENT_ID',
        redirect_uri: '/callback.html'
    });

    SC.connect(function () {
        SC.get('/me', function (me) {
            alert('Hello, ' + me.username);
        });
    });

    var dashTracks = SC.get('/me/activities/tracks/affiliated');
}