Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (45 loc) · 1.57 KB
/
Copy pathindex.js
File metadata and controls
56 lines (45 loc) · 1.57 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const { default: Axios } = require("axios");
const PORT = process.env.PORT || "5543";
const SPOTIFY_CLIENT_SECRET = process.env.SPOTIFY_CLIENT_SECRET;
const SPOTIFY_CLIENT_ID = process.env.SPOTIFY_CLIENT_ID;
if (!SPOTIFY_CLIENT_SECRET || !SPOTIFY_CLIENT_ID) {
console.log(
"SPOTIFY_CLIENT_SECRET or SPOTIFY_CLIENT_ID environment variable is not set!"
);
process.exit(1);
}
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/swap", async (req, res) => {
const { code } = req.body;
const params = new URLSearchParams();
params.append("grant_type", "authorization_code");
params.append("code", code);
params.append("redirect_uri", "http://localhost:5543/callback");
params.append("client_secret", SPOTIFY_CLIENT_SECRET);
params.append("client_id", SPOTIFY_CLIENT_ID);
const { data } = await Axios.post(
"https://accounts.spotify.com/api/token",
params
);
return res.send(data);
});
app.post("/refresh", async (req, res) => {
const { refresh_token } = req.body;
const params = new URLSearchParams();
params.append("grant_type", "refresh_token");
params.append("refresh_token", refresh_token);
params.append("client_secret", SPOTIFY_CLIENT_SECRET);
params.append("client_id", SPOTIFY_CLIENT_ID);
const { data } = await Axios.post(
"https://accounts.spotify.com/api/token",
params
);
return res.send(data);
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT} 🚀`);
});