forked from bpmutter/RappaMappa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
25 lines (21 loc) · 895 Bytes
/
utils.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
const { spotifyClientSecret, spotifyClientId } = require("./config");
const fetch = require("node-fetch");
const qs = require('qs');
const asyncHandler = (handler) => (req, res, next) =>
handler(req, res, next).catch(next);
const getSpotifyAccessToken = async () => {
const accountCredential = `${spotifyClientId}:${spotifyClientSecret}`;
let buff = new Buffer(accountCredential);
let b64encodedAccountInfo = buff.toString("base64");
const data = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${b64encodedAccountInfo}`,
},
body: qs.stringify({ grant_type: "client_credentials" }),
});
const token = await data.json(); //full token object
return token;
}
module.exports = { asyncHandler, getSpotifyAccessToken}