Creating a Set With Soundcloud's API - javascript

I'm using Javascript to create a web app with Soundcloud's API for my portfolio. At my current stage I need to be able to create a new set (aka playlist). I was using the sample code from Soundcloud's docs:
SC.connect(function() {
var tracks = [22448500, 21928809].map(function(id) { return { id: id } });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }
});
});
But I'm getting a 422 error:
Unprocessable Entity - The request looks alright, but one or more of
the parameters looks a little screwy. It's possible that you sent data
in the wrong format (e.g. an array where we expected a string).
But it does not look like anything's missing.

The call to the SoundCloud API requires a callback function in addition to the playlist title and tracks. Your code should look like this:
SC.connect(function() {
var tracks = [22448500, 21928809].map(function(id) { return { id: id } });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }, function(response){
console.log(response)
}
});
});
Their example is, unfortunately, wrong.

Related

Is there a way to fix resource badcfg get error in console

Am getting this error in my console
http://errors.angularjs.org/1.5.3/$resource/badcfg?p0=object&p1=array&p2=GET&p3=https%3A%2F%2Fwww.2tag2.com%2F%2Frequests%2Fapi.php%3Faction%3DgetChat%26id%3D1
Help me resolve this. This is my code service.js
.factory('A', ['$resource',
function($resource) {
;
return {
Chat: $resource(site_url + '/requests/api.php?action=:action&uid1=:uid1&uid2=:uid2', {
action: '#action',
uid1: '#uid1',
uid2: '#uid2'
}),
};
}
])
“To resolve this error, make sure your $resource configuration matches the actual format of the data returned from the server.”
In your case, since the api returns an array, change action=get to action=query.
As stated in the debug:
“By default, all resource actions expect objects, except query which expects arrays.”
Try the following:
var Chat = $resource(
site_url +
'/requests/api.php?action=:action&uid1=:uid1&uid2=:uid2',
{
action: '#action',
uid1: '#uid1',
uid2: '#uid2'
});
var chats = Chat.query({ action: 'getChat', uid1: 1});
chats.$promise.then(function() {
console.log(chats);
return chats;
});
Consider building your API Restfull. In your example above this will mean some altering of your code and api architecture.
To retrieve your chats it is better to build your API calls like below:
Retrieving all chats:
site_url + '/v1/chats'
Retrieving a chat by id
site_url + '/v1/chats/:chatId'

(Reddit API) How to get a list of subreddits user is subscribed to, using snoowrap?

So I'm using snoowrap to write a Chrome extension that gets a list of subreddits the user is subscribed, and subscribes to them on a different account.
I'm trying to get the list of subreddits currently but can't figure out how to do it. I've tried simply getting the JSON from https://www.reddit.com/subreddits/mine.json, which returns an empty object (persumably because no auth) and I have no idea how to do it via snoowrap. I looked through the documentation and can't find an option for it.
My code:
document.addEventListener('DOMContentLoaded', function() {
var login = document.getElementById('login');
login.addEventListener('click', function() {
const r = new snoowrap({
userAgent: '???',
clientId: '<id>',
clientSecret: '<clientsecret>',
username: '<username-here>',
password: '<password-here>'
});
r.getHot().map(post => post.title).then(console.log);
});
var getSubs = document.getElementById('get-subs');
getSubs.addEventListener('click', function() {
fetch('https://www.reddit.com/subreddits/mine.json')
.then(function(data) {
console.log(JSON.stringify(data));
})
.catch(function(error) {
console.log('error');
});
});
});
Not sure how else to try. Anyone have suggestions? I'd like to use snoowrap for this ideally.
When using snoowrap as API wrapper, after connecting to the api with:
const r = new snoowrap({...});
They provide a function for getting your own subscribed subreddits:
r.getSubscriptions();
This will return a Listing Object, which you can use like an Array.

How to access the trello API from within a powerup?

How can I make queries against the trello API from within a powerup? This seems like such an obvious question but it doesn't seem to be covered that I can find.
My simple powerup looks like this so far:
var boardButtonCallback = function(t){
return t.popup({
title: 'Tools',
items: [
{
text: 'Hide Duplicates',
callback: function(t){
var cardQueryCb = function(result){
console.log(result);
}
var cardQ = 'https://trello.com/1/boards/[board_id]/cards/all';
fetch(cardQ).then(function(response) {
return response.json();
}).then(function(data) {
console.log(data);
});
return t.cards('id', 'name')
.then(cardQueryCb);
}
}
]
});
};
TrelloPowerUp.initialize({
'board-buttons': function(t, options){
return [{
text: 'Duplicates',
callback: boardButtonCallback
}];
}
});
The response object after the call to fetch says the call is unauthorized.
I would have thought that calling this code from within the context of the power up would be considered authorized. While I'm logged into trello, I'm able to put that address into my browser and get a valid response - how come the javascript call doesn't also result in a valid response?
More importantly, how can I get a successful response from that URL?
Since your power-up is run through an iframe, it's not actually coming from the Trello page itself so you need to specify your API key and token in the GET URL.
Example:
https://api.trello.com/1/boards/560bf4298b3dda300c18d09c?fields=name,url&key={YOUR-API-KEY}&token={AN-OAUTH-TOKEN}
The info for getting your API key and token can be found here: https://trello.readme.io/v1.0/reference#api-key-tokens

How to Create a Soundcloud playlist ( set ) using javascript?

I am trying to use Soundcloud api for my application where user can create his/her own playlist of track . As a test case the example I am testing is almost exactly taken from the Soundcloud dev docs. below is my code
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
// initialize client with app credentials
SC.initialize({
client_id: 'MY_CLIENT_ID',
redirect_uri: 'http://localhost.local/~****/sc/callback.html'
});
// initiate auth popup and create new playlist
SC.connect(function() {
SC.get('/me', function(me) {
console.log(me.username);
});
var tracks = [12573606].map(function(id) { return { id: id }; });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }
});
});
I already searched so many thing's in google but nothing helped me, actually i need temporary playlist so when user logout or close the browser playlist also delete . Any help would be appreciable.. thanx
Can you try sending the client_secret => 'YOUR_CLIENT_SECRET' as part of the initialize call
It doesn't look like there is such a thing in soundcloud to have a temporary playlist, you will need to delete the playlist on exit....
The best guess would be to see how to format your url to match when someone clicks on the delete button which you can find here: http://help.soundcloud.com/customer/portal/articles/282512-how-do-i-delete-tracks-from-my-account- I don't have an account so I can't test this part.
The key to creating the playlist is having the right track ids. It is possible if your having an issue that the track id doesn't exist and so it doesn't get added to the playlist.
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
// initialize client with app credentials
SC.initialize({
client_id: 'MY_CLIENT_ID',
redirect_uri: 'http://localhost.local/~****/sc/callback.html'
});
// initiate auth popup and create new playlist
SC.connect(function() {
SC.get('/me', function(me) {
console.log(me.username);
});
var tracks = [12573606].map(function(id) { return { id: id }; });
SC.post('/playlists', {
playlist: { title: 'My Playlist', tracks: tracks }
});
//As you can see it is a simple array of ids of track
tracks = [21778201, 22448500, 21928809];
//To get all playlists remove limit and search each playlist name and grab id
SC.get('/me/playlists', { limit: 1 }, function(playlist) {
SC.put(playlist.uri, { playlist: { tracks: tracks } });
});
//Then to get all the tracks, please substitute the playlist id for 1234323:
SC.get('/playlists/1234323', function(playlist) {
for (var i = 0; i < playlist.tracks.length; i++) {
console.log(playlist.tracks[i].length);
}
});
});

Soundcloud API list my stream

I'm using the JavaScript SDK for the Soundcloud API and trying to populate the top 10 tracks from authenticated users stream.
Currently I am implementing this with:
SC.get('/tracks', { limit: '10'}, function(tracks) {
//Do stuff with tracks
});
However, this only returns the most recent 10 tracks/sounds uploaded to SoundCloud. Is there an easy way to populate my stream?
I was looking for the same thing, but there is no way to get the complete user's stream. You can get the users activities from
https://api.soundcloud.com/me/activities.json
But there are not all songs listed from your stream. I hope the SoundCloud devs will make that thing better in the future.
Try to use this code
fetch: function () {
var self = this;
SC.get("/tracks", { limit: '10'}, function (data) {
self.tracks = $.map(data, function (track) {
return {
title: track.title
};
});
self.attachTemplate();
});
}
soundcloud.init({
template: $('#tracks-template').html(),
container: $('ul.soundcloud')
});
And show the item title in your HTML page.

Categories

Resources