无法通过API的SoundCloud使用JavaScript曲目添加到组 [英] Can't add a track to a group via soundcloud API using javascript

查看:190
本文介绍了无法通过API的SoundCloud使用JavaScript曲目添加到组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经克隆从GitHub的JavaScript SDK,比Firefox15.0不高兴其他的,我能得到什么,我想要的。 (曲目列表,组列表)。

我希望能够加入我的赛道之一,我的集团之一,(到目前为止我只有每一个,保持测试简单)的的控制台让我加入(PUT的 https://api.soundcloud.com/groups/92779/contributions/58889291 )和一组(具有相同的URI删除)删除相同的轨道,但我不使用JavaScript SDK jQuery的例子。 (我开始觉得这是jquery.sc.api.js,但我不知道我寻求帮助。)

这是我砍死在一起至今:(以及我做了很多,但我已经把它降低到仅仅强调这一个例子。)

index.html的

 <!DOCTYPE HTML>
< HTML和GT;
  < HEAD><间的charset =UTF-8/>
<标题> SC.js.test< /标题>
<脚本> VAR FireFox15_error ='错误:的ReferenceError:ID没有被定义\\
源文件:jquery.tmpl.js行:126
< / SCRIPT>
<风格类型=文/ CSS> .hidden {visibility:hidden的; }< /风格>
< /头>
  <身体GT;
    < D​​IV ID =容器>
    < / DIV>
    < D​​IV ID =轨道级=隐藏>
    < H2>您的曲目< / H>
      < UL ID =曲目列表>
      < / UL>
        添加按钮似乎'工作'如果轨道已经在一组,但只是重定向到登录页面,如果它不是(这是一个迹象,表明令牌不被用PUT送?)
    < / DIV>
<! - 我我只是老想这些迁入的< HEAD> ? - >
<脚本类型=文/ JavaScript的字符集=utf-8SRC =jquery_1.7.1_min.js>< / SCRIPT>
<脚本类型=文/ JavaScript的字符集=utf-8SRC =jquery.tmpl.js>< / SCRIPT>
<脚本类型=文/ JavaScript的字符集=utf-8SRC =jquery.sc.api.js>< / SCRIPT>
<脚本类型=文/ JavaScript的字符集=utf-8SRC =main.js>< / SCRIPT>
    <! - 模板 - >
    <脚本ID =轨迹TYPE =text / html的>
      <李数据磁道-ID =$ {ID}ID =$ {ID}> $ {$ I})LT; A HREF =$ {permalink_url}> $ {标题}< / A>($ {ID})LT; /李>
    < / SCRIPT>
< D​​IV ID =调试级=隐藏>
    < H3>请求/问题< / H3 GT&;
<&OL GT;
<立GT; api.put('/我',{说明:猜猜使用新的API函数是谁?'}); //请加该< /李>
<立GT;为什么资深​​大律师有不同的字符串比track_id从组中删除的曲目? < /李>
< / OL>
< / DIV>
< /身体GT;< / HTML>

main.js

 (函数($){
   VAR soundCloudApiKey ='should_be_able_to_create_a_key_for_(locked_to)_each_user';
   VAR USER_ID ='';
  VAR API = $ .sc.api(soundCloudApiKey,{
    onAuthSuccess:功能(用户容器){
        USER_ID = user.id;
      $('<跨度类=用户名>登录为:其中;强>'+ user.username +'< / STRONG>('+ USER_ID +')LT; / A>')。prependTo(容器);
    }
  });
  (文档)$ .bind($。sc.api.events.AuthSuccess,函数(事件){
  $(本)。点击(函数(数据){
    。数据preventDefault();
    VAR发件人= window.event.srcElement;
    如果(Sender.id =='add_track_submit'){
        。VAR GROUP_ID = $('#GROUP_ID)VAL();
        。VAR track_id = $('#track_id)VAL();
        警报('尝试添加曲目+ track_id +'到组'+ GROUP_ID);
        //api.put('/groups/'+ GROUP_ID +'/来稿/+ track_id); // http://developers.soundcloud.com/docs/api/guide#contributing-group
        api.put('/组/+ GROUP_ID +'/来稿/+ track_id,功能(回复,E){//试图让为什么这不工作的一些反馈
                    //这似乎是使用GET,而不是一个PUT或POST
        //api.post('/groups/'+ GROUP_ID +'/来稿/+ track_id,功能(回复,E){//也似乎不工作
            如果(E){执行console.log('错误:'+ E); }
            的console.log('曲目添加回复+回复);
            警报('轨道'+ track_id +添加到组+ GROUP_ID);
        });
    }
   返回false;
  });
   api.get('/ ME /曲目',函数(数据){
      //你可以使用新的jQuery模板生成的曲目列表
      $('#轨')渲染(数据).appendTo(#曲目列表);
      //常规,如果Firefox15.0不恨它,(谷歌浏览器18.0.1025.162/JavaScript V8 3.8.9.18作品)
      $ .MAP(数据,功能(跟踪,我){
            my_group_id = 92779; //肮脏的黑客,而我试图让这个工作只有一个轨道,一组
            VAR ADD_BUTTON ='<跨度ID =ADD_BUTTON><表ID =add_track><输入ID =GROUP_ID类型=隐藏的名字=GROUP_ID值=+ my_group_id +' /> \\
<输入类型=隐藏ID =track_idNAME =track_idVALUE =+ track.id +'/> \\
<输入类型=提交ID =add_track_submitNAME =增加VALUE =添加+ track.title +'到组'+ my_group_id +'/>< /表>< / SPAN> ';
                                $('#轨道列表>#+ track.id).append(ADD_BUTTON);
    });
    $('格')removeAttr(类,隐藏)。
  });
});
})(jQuery的);


解决方案

发现在SDK中的错误和固定它:的https://github.com/alexxroche/SoundCloud-API-jQuery-plugin/blob/master/jquery.sc.api.js - 我的code没有必要改变。

I've cloned the javascript SDK from github, and other than Firefox15.0 being unhappy, I'm able to GET anything that I want. (Track lists, group lists).

I want to be able to add one of my tracks to one of my groups, (so far I've only got one of each, to keep testing simple.) The console lets me add (PUT https://api.soundcloud.com/groups/92779/contributions/58889291) and remove the same track from a group (DELETE with the same URI), but my not with the javascript SDK jQuery example. (I'm starting to think that it is jquery.sc.api.js, but as I'm not sure I'm asking for help.)

This is what I've hacked together so far: (well I did a lot more, but I've cut it down to highlight just this one example.)

index.html

<!DOCTYPE html>
<html>
  <head><meta charset="utf-8" />
<title>SC.js.test</title>
<script> var FireFox15_error = 'Error: ReferenceError: id is not defined \
Source File: jquery.tmpl.js Line: 126'
</script>
<style type="text/css">.hidden { visibility: hidden; }</style>
</head>
  <body>
    <div id="container">
    </div>
    <div id="tracks" class="hidden">
    <h2>Your tracks</h2>
      <ul id="track-list">
      </ul>
        The Add button seems 'to work' if the track is already in the group, but just redirects to the login page if it isn't (is this a sign that the token isn't being sent with the PUT ?)
    </div>
<!-- am I'm just old wanting to move these into the <head> ? -->
<script type="text/javascript" charset="utf-8" src="jquery_1.7.1_min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.tmpl.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.sc.api.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
    <!-- Templates -->
    <script id="track" type="text/html">
      <li data-track-id="${id}" id="${id}">${$i}) <a href="${permalink_url}">${title}</a>(${id})</li>
    </script>
<div id="debug" class="hidden">
    <h3>Requests/Questions</h3>
<ol>
<li>api.put('/me', { "description": 'Guess who is using the new API function?' }); //please add this</li>
<li> why does sc have a different string than the track_id for removing tracks from a group?  </li>
</ol>
</div>
</body></html>

main.js

(function($) {
   var soundCloudApiKey = 'should_be_able_to_create_a_key_for_(locked_to)_each_user';
   var user_id = '';
  var api = $.sc.api(soundCloudApiKey, {
    onAuthSuccess: function(user, container) {
        user_id = user.id;
      $('<span class="username">Logged in as: <strong>' + user.username + '</strong> (' + user_id + ')</a>').prependTo(container);
    }
  });
  $(document).bind($.sc.api.events.AuthSuccess, function(event) {
  $(this).click(function(data) {
    data.preventDefault();
    var Sender = window.event.srcElement;
    if(Sender.id == 'add_track_submit'){
        var group_id = $('#group_id').val();
        var track_id = $('#track_id').val();
        alert('TRYING to add track ' + track_id + ' to group ' + group_id);
        //api.put('/groups/' + group_id + '/contributions/' + track_id); // http://developers.soundcloud.com/docs/api/guide#contributing-group
        api.put('/groups/' + group_id + '/contributions/' + track_id, function(reply, e){ //trying to get some feedback on why this does not work
                    // It seems to be using a GET rather than a PUT or POST
        //api.post('/groups/' + group_id + '/contributions/' + track_id, function(reply, e){ //also does not seem to work
            if(e){ console.log('err:' + e); }
            console.log('track add reply' + reply);
            alert('track ' + track_id + ' added to group ' + group_id);
        });
    }
   return false;
  });
   api.get('/me/tracks', function(data) {
      // you can use new jQuery templating for generating the track list
      $('#track').render(data).appendTo("#track-list");
      //groovy, if Firefox15.0 did not hate it, (works in Google Chrome 18.0.1025.162/JavaScript V8 3.8.9.18)
      $.map( data, function(track, i){
            my_group_id = 92779; //dirty hack while I try to get this working with just one track and one group
            var add_button = '<span id="add_button"><form id="add_track"><input id="group_id" type="hidden" name="group_id" value="' + my_group_id + '" /> \
<input type="hidden" id="track_id" name="track_id" value="' + track.id + '" /> \
<input type="submit" id="add_track_submit" name="add" value="Add ' + track.title + ' to group ' + my_group_id + '" /></form></span>';
                                $('#track-list>#' + track.id).append(add_button);
    });
    $('div').removeAttr("class", "hidden");
  });
});
})(jQuery);

解决方案

Found the bug in the SDK and fixed it: https://github.com/alexxroche/SoundCloud-API-jQuery-plugin/blob/master/jquery.sc.api.js - my code didn't need changing.

这篇关于无法通过API的SoundCloud使用JavaScript曲目添加到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆