Browse Source

sc api test

bach 1 year ago
parent
commit
378985b8db

+ 107 - 35
sites/all/themes/gui/perfarttimeline/js/libs/soundcloudcustomplayer/js/pa-sc-player.js

@@ -52,9 +52,11 @@
       secureDocument = (document.location.protocol === 'https:'),
       // convert a SoundCloud resource URL to an API URL
       scApiUrl = function(url, apiKey) {
+        // var resolver = ( secureDocument || (/^https/i).test(url) ? 'https' : 'http') + '://api.' + domain + '/resolve?url=',
+        //     params = 'format=json&consumer_key=' + apiKey +'&callback=?';
         var resolver = ( secureDocument || (/^https/i).test(url) ? 'https' : 'http') + '://api.' + domain + '/resolve?url=',
-            params = 'format=json&consumer_key=' + apiKey +'&callback=?';
-
+            params = 'format=json&callback=?';
+        // debugger;
         // force the secure url in the secure environment
         if( secureDocument ) {
           url = url.replace(/^http:/, 'https:');
@@ -258,40 +260,110 @@
             playerObj = {node: $player, tracks: []},
             loadUrl = function(link) {
               var apiUrl = scApiUrl(link.url, apiKey);
-              $.getJSON(apiUrl, function(data) {
-                // log('data loaded', link.url, data);
-                index += 1;
-								// added by Bach for MJ
-								data.href = link.href;
-                if(data.tracks){
-                  // log('data.tracks', data.tracks);
-                  playerObj.tracks = playerObj.tracks.concat(data.tracks);
-                }else if(data.duration){
-                  // a secret link fix, till the SC API returns permalink with secret on secret response
-                  data.permalink_url = link.url;
-                  // if track, add to player
-                  playerObj.tracks.push(data);
-                }else if(data.creator){
-                  // it's a group!
-                  links.push({url:data.uri + '/tracks'});
-                }else if(data.username){
-                  // if user, get his tracks or favorites
-                  if(/favorites/.test(link.url)){
-                    links.push({url:data.uri + '/favorites'});
-                  }else{
-                    links.push({url:data.uri + '/tracks'});
-                  }
-                }else if($.isArray(data)){
-                  playerObj.tracks = playerObj.tracks.concat(data);
-                }
-                if(links[index]){
-                  // if there are more track to load, get them from the api
-                  loadUrl(links[index]);
-                }else{
-                  // if loading finishes, anounce it to the GUI
-                  playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
+              // $.getJSON(apiUrl, function(data) {
+              //   // log('data loaded', link.url, data);
+              //   index += 1;
+							// 	// added by Bach for MJ
+							// 	data.href = link.href;
+              //   if(data.tracks){
+              //     // log('data.tracks', data.tracks);
+              //     playerObj.tracks = playerObj.tracks.concat(data.tracks);
+              //   }else if(data.duration){
+              //     // a secret link fix, till the SC API returns permalink with secret on secret response
+              //     data.permalink_url = link.url;
+              //     // if track, add to player
+              //     playerObj.tracks.push(data);
+              //   }else if(data.creator){
+              //     // it's a group!
+              //     links.push({url:data.uri + '/tracks'});
+              //   }else if(data.username){
+              //     // if user, get his tracks or favorites
+              //     if(/favorites/.test(link.url)){
+              //       links.push({url:data.uri + '/favorites'});
+              //     }else{
+              //       links.push({url:data.uri + '/tracks'});
+              //     }
+              //   }else if($.isArray(data)){
+              //     playerObj.tracks = playerObj.tracks.concat(data);
+              //   }
+              //   if(links[index]){
+              //     // if there are more track to load, get them from the api
+              //     loadUrl(links[index]);
+              //   }else{
+              //     // if loading finishes, anounce it to the GUI
+              //     playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
+              //   }
+              // });
+
+              // https://developers.soundcloud.com/docs/api/guide#authentication
+              // # obtain the access token
+              // $ curl -X POST "https://api.soundcloud.com/oauth2/token" \
+              //     -H  "accept: application/json; charset=utf-8" \
+              //     -H  "Content-Type: application/x-www-form-urlencoded" \
+              //     --data-urlencode "grant_type=client_credentials" \
+              //     --data-urlencode "client_id=YOUR_CLIENT_ID" \
+              //     --data-urlencode "client_secret=YOUR_CLIENT_SECRET"
+              $.ajax({
+                method: 'POST', 
+                url: 'https://api.soundcloud.com/oauth2/token',
+                data: {
+                  'grant_type' : "client_credentials",
+                  'client_id' : "965bd4363fdd909723749b003be67125",
+                  'client_secret': "bb68647335a47f104a86dcddf4e70fa8"
+                },
+                headers: {
+                  "accept" : "application/json; charset=utf-8",
+                  "Content-Type" : "application/x-www-form-urlencoded",
+                  "Authorization": "OAuth 965bd4363fdd909723749b003be67125"
                 }
-             });
+              });
+
+              // $.ajax({
+              //   "dataType": "json",
+              //   "url": apiUrl,
+              //   "data": {},
+              //   "headers": {
+              //     "Authorization": "OAuth " + apiKey
+              //   },
+              //   "success": function(data) {
+              //     // log('data loaded', link.url, data);
+              //     index += 1;
+              //     // added by Bach for MJ
+              //     data.href = link.href;
+              //     if(data.tracks){
+              //       // log('data.tracks', data.tracks);
+              //       playerObj.tracks = playerObj.tracks.concat(data.tracks);
+              //     }else if(data.duration){
+              //       // a secret link fix, till the SC API returns permalink with secret on secret response
+              //       data.permalink_url = link.url;
+              //       // if track, add to player
+              //       playerObj.tracks.push(data);
+              //     }else if(data.creator){
+              //       // it's a group!
+              //       links.push({url:data.uri + '/tracks'});
+              //     }else if(data.username){
+              //       // if user, get his tracks or favorites
+              //       if(/favorites/.test(link.url)){
+              //         links.push({url:data.uri + '/favorites'});
+              //       }else{
+              //         links.push({url:data.uri + '/tracks'});
+              //       }
+              //     }else if($.isArray(data)){
+              //       playerObj.tracks = playerObj.tracks.concat(data);
+              //     }
+              //     if(links[index]){
+              //       // if there are more track to load, get them from the api
+              //       loadUrl(links[index]);
+              //     }else{
+              //       // if loading finishes, anounce it to the GUI
+              //       playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
+              //     }
+              //   },
+              //   "error": function(errorThrown) {
+              //     console.error(JSON.stringify(errorThrown.error()));
+              //   }
+              // });
+              
            };
         // update current API key
         apiKey = key;