2023-05-27 20:42:31 +01:00
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { default: Axios } = require("axios");
|
2020-06-02 15:34:24 +01:00
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
const PORT = process.env.PORT || "5543";
|
2020-06-02 15:34:24 +01:00
|
|
|
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) {
|
2023-05-27 20:42:31 +01:00
|
|
|
console.log(
|
|
|
|
"SPOTIFY_CLIENT_SECRET or SPOTIFY_CLIENT_ID environment variable is not set!"
|
|
|
|
);
|
2020-06-02 15:34:24 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
app.post("/swap", async (req, res) => {
|
2020-06-02 15:34:24 +01:00
|
|
|
const { code } = req.body;
|
|
|
|
|
|
|
|
const params = new URLSearchParams();
|
2023-05-27 20:42:31 +01:00
|
|
|
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);
|
2020-06-02 15:34:24 +01:00
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
const { data } = await Axios.post(
|
|
|
|
"https://accounts.spotify.com/api/token",
|
|
|
|
params
|
|
|
|
);
|
2020-06-02 15:34:24 +01:00
|
|
|
|
|
|
|
return res.send(data);
|
|
|
|
});
|
|
|
|
|
2023-05-27 20:42:31 +01:00
|
|
|
app.post("/refresh", async (req, res) => {
|
2020-06-02 15:34:24 +01:00
|
|
|
const { refresh_token } = req.body;
|
|
|
|
|
|
|
|
const params = new URLSearchParams();
|
2023-05-27 20:42:31 +01:00
|
|
|
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
|
|
|
|
);
|
2020-06-02 15:34:24 +01:00
|
|
|
|
|
|
|
return res.send(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`Server listening on ${PORT} 🚀`);
|
|
|
|
});
|