2014-05-14 10:27:27 +02:00
|
|
|
/**
|
|
|
|
* This is an example of a basic node.js script that performs
|
|
|
|
* the Client Credentials oAuth2 flow to authenticate against
|
|
|
|
* the Spotify Accounts.
|
|
|
|
*
|
|
|
|
* For more information, read
|
2014-06-17 10:25:03 +02:00
|
|
|
* https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
|
2014-05-14 10:27:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var request = require('request'); // "Request" library
|
|
|
|
|
|
|
|
var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id
|
2014-06-03 13:26:18 +02:00
|
|
|
var client_secret = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your client secret
|
2014-05-14 10:27:27 +02:00
|
|
|
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri
|
|
|
|
|
|
|
|
// your application requests authorization
|
|
|
|
var authOptions = {
|
|
|
|
url: 'https://accounts.spotify.com/api/token',
|
|
|
|
headers: {
|
2014-06-03 13:26:18 +02:00
|
|
|
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
|
2014-05-14 10:27:27 +02:00
|
|
|
},
|
|
|
|
form: {
|
|
|
|
grant_type: 'client_credentials'
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
|
|
|
|
request.post(authOptions, function(error, response, body) {
|
|
|
|
if (!error && response.statusCode === 200) {
|
|
|
|
|
|
|
|
// use the access token to access the Spotify Web API
|
|
|
|
var token = body.access_token;
|
|
|
|
var options = {
|
|
|
|
url: 'https://api.spotify.com/v1/users/jmperezperez',
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'Bearer ' + token
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
request.get(options, function(error, response, body) {
|
|
|
|
console.log(body);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|