-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (47 loc) · 1.36 KB
/
app.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
import dotenv from "dotenv";
import axios from "axios";
dotenv.config();
const credentials = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
refresh_token: process.env.REFRESH_TOKEN,
};
let accessToken = "";
const refreshToken = async () => {
const { client_id, client_secret, refresh_token } = credentials;
const authOptions = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${Buffer.from(
`${client_id}:${client_secret}`
).toString("base64")}`,
},
data: new URLSearchParams({
grant_type: "refresh_token",
refresh_token: refresh_token ?? "",
}).toString(),
};
try {
const response = await axios(
"https://accounts.spotify.com/api/token",
authOptions
);
console.log("Response data:", response.data);
if (response.data.access_token) {
accessToken = response.data.access_token;
console.log("New access token:", accessToken);
} else {
throw new Error("No access token received.");
}
} catch (error) {
console.error("Error refreshing token:", error.message);
}
};
const main = async () => {
console.log("main refresh", credentials.refresh_token);
console.log("main before", accessToken);
await refreshToken();
console.log("main after", accessToken);
};
main();