-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.js
122 lines (101 loc) · 3.08 KB
/
spotify.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const fetch = require('node-fetch');
const configData = require("./config.json");
const CLIENT_ID = configData.clientId;
const CLIENT_SECRET = configData.clientSecret;
// const TOKEN = configData.testToken;
let TOKEN = false
let prevTokenTimestamp = 0
const express = require("express")
const app = express()
app.use(express.static("static"))
// Refresh the token automatically!
app.use((req, res, next)=>{
refreshTokenIfNecessary()
.then(()=>next())
})
async function refreshTokenIfNecessary() {
if (Date.now() - prevTokenTimestamp > 3550) {
return refreshToken()
} else {
return false
}
}
async function refreshToken() {
TOKEN = await getSpotifyToken(CLIENT_ID, CLIENT_SECRET)
prevTokenTimestamp = Date.now()
console.log("Token Refreshed at", Date.now())
return true
}
async function getSpotifyToken(id, secret) {
return fetch("https://accounts.spotify.com/api/token", {
method:"POST",
headers:{
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')
},
body: encodeURIComponent("grant_type") + "=" + encodeURIComponent("client_credentials")
})
.then(res=>res.json())
}
async function init() {
const token = await getSpotifyToken(CLIENT_ID, CLIENT_SECRET)
console.log(token)
}
// init()
// const taylor = "06HL4z0CvFAxyc27GXpf02"
// fetch(`https://api.spotify.com/v1/artists/${taylor}`, {
// method:"GET",
// headers:{
// 'Content-Type': 'application/json',
// 'Authorization': 'Bearer ' + TOKEN
// }
// })
// .then(res=>res.json())
// .then(console.log)
// fetch(`https://api.spotify.com/v1/search/?q=test&type=track`, {
// method:"GET",
// headers:{
// 'Content-Type': 'application/json',
// 'Authorization': 'Bearer ' + TOKEN
// }
// })
// .then(res=>res.json())
// .then(data=>console.log(JSON.stringify(data)))
// const song="4IRpbTrTCHkP3aeRO48tZs"
// fetch(`https://api.spotify.com/v1/tracks/${song}`, {
// method:"GET",
// headers:{
// 'Content-Type': 'application/json',
// 'Authorization': 'Bearer ' + TOKEN
// }
// })
// .then(res=>res.json())
// .then(data=>console.log(JSON.stringify(data, null, 4)))
// fetch(`https://api.spotify.com/v1/audio-features/${song}`, {
// method:"GET",
// headers:{
// 'Content-Type': 'application/json',
// 'Authorization': 'Bearer ' + TOKEN
// }
// })
// .then(res=>res.json())
// .then(data=>console.log(JSON.stringify(data, null, 4)))
// init()
// var authOptions = {
// url: 'https://accounts.spotify.com/api/token',
// headers: {
// 'Authorization': 'Basic ' + Buffer.from(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')
// },
// form: {
// grant_type: 'client_credentials'
// },
// json: true
// };
// request.post(authOptions, function(error, response, body) {
// if (!error && response.statusCode === 200) {
// var token = body.access_token;
// }
// });
// fetch("https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?album_type=SINGLE&offset=20&limit=10")
// .then(res=>res.json())
// .then(text=>console.log(text))