Created
July 31, 2017 04:58
-
-
Save 45deg/d5b257ad9a8a644e4e627dca8f071f5d to your computer and use it in GitHub Desktop.
Accessing SoundCloud HTTP API by Node.js (> 7.x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
References: | |
https://developers.soundcloud.com/docs/api/guide | |
https://developers.soundcloud.com/docs/api/reference | |
*/ | |
const fetch = require('node-fetch'); | |
const readline = require('readline'); | |
const FormData = require('form-data'); | |
const util = require('util'); | |
(async function main(){ | |
let rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
let formData = new FormData(); | |
formData.append('grant_type', 'password'); | |
for (let name of ['client_id', 'client_secret', 'username', 'password']) { | |
formData.append(name, await ask(name + ': ', rl)); | |
} | |
rl.close(); | |
let tokenResp = await fetch('https://api.soundcloud.com/oauth2/token', | |
{ method: 'POST', body: formData }); | |
let tokenJson = await tokenResp.json(); | |
let accessToken = tokenJson.access_token; | |
let activitiesResp = await fetch('https://api.soundcloud.com/me/activities?oauth_token=' + accessToken); | |
let activities = await activitiesResp.json(); | |
console.log('Result: '); | |
console.log(util.inspect(activities, { depth: null })); | |
})(); | |
function ask(message, rl){ | |
return new Promise(function(resolve, reject) { | |
rl.question(message, function(answer){ | |
resolve(answer); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "soundcloud-node-example", | |
"version": "1.0.0", | |
"description": "Accessing SoundCloud HTTP API by Node.js (> 7.x)", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "45deg", | |
"license": "WTFPL", | |
"dependencies": { | |
"form-data": "^2.2.0", | |
"node-fetch": "^1.7.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment