-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
37 lines (31 loc) · 1013 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var request = require('request');
var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/token', function(req, resp) {
resp.header('Access-Control-Allow-Origin', '*');
resp.header('Access-Control-Allow-Headers', 'X-Requested-With');
var client_id = process.env.SPOTIFY_CLIENT_ID;
var client_secret = process.env.SPOTIFY_CLIENT_SECRET;
// your application requests authorization
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
headers: {
Authorization:
'Basic ' +
new Buffer(client_id + ':' + client_secret).toString('base64')
},
form: {
grant_type: 'client_credentials'
},
json: true
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
resp.json({ token: body.access_token });
}
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});