-
Notifications
You must be signed in to change notification settings - Fork 66
/
server.js
executable file
·69 lines (47 loc) · 1.56 KB
/
server.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Spotify API Token server
Esta aplicación únicamente toma el CLIENTID y CLIENTSecret
que brinda spotify, para obtener el token mediante una petición
POST desde el front-end.
*/
const express = require('express');
const request = require('request');
const path = require('path');
const app = express();
const publicPath = path.resolve(__dirname, '../public');
const port = process.env.PORT || 3000;
app.use(express.static(publicPath));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/spotify/:client_id/:client_secret', (req, resp) => {
let client_id = req.params.client_id;
let client_secret = req.params.client_secret;
let spotifyUrl = 'https://accounts.spotify.com/api/token';
var authOptions = {
url: spotifyUrl,
headers: {
Authorization: 'Basic ' + new Buffer(client_id + ':' + client_secret).toString('base64')
},
form: {
grant_type: 'client_credentials'
},
json: true
};
request.post(authOptions, (err, httpResponse, body) => {
if (err) {
return resp.status(400).json({
ok: false,
mensaje: 'No se pudo obtener el token',
err
})
}
resp.json(body);
});
});
app.listen(port, (err) => {
if (err) throw new Error(err);
console.log(`Servidor corriendo en puerto ${ port }`);
});