-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
226 lines (184 loc) · 6.33 KB
/
test.js
File metadata and controls
226 lines (184 loc) · 6.33 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// const client_id = "3edfcb2cdb144a9796a8c39f5ce3730a";
// const client_secret = '2693336dd1044b1989fe26ad8f42500f';
// const express = require('express');
// const axios = require('axios'); // You'll need: npm install axios
// const querystring = require('querystring');
// require('dotenv').config(); // Best practice: npm install dotenv
// const app = express();
// const redirect_uri = 'http://127.0.0.1:8888/callback';
// app.get('/login', function(req, res) {
// const state = '1234567890123456'; // Simplified for this example
// const scope = 'user-read-private user-read-email';
// res.redirect('https://accounts.spotify.com/authorize?' +
// querystring.stringify({
// response_type: 'code',
// client_id: client_id,
// scope: scope,
// redirect_uri: redirect_uri,
// state: state
// }));
// });
// app.get('/callback', async function(req, res) {
// const code = req.query.code || null;
// const state = req.query.state || null;
// if (state === null) {
// res.redirect('/#error=state_mismatch');
// } else {
// try {
// // This is where 'app' executes the actual token exchange
// const response = await axios({
// method: 'post',
// url: 'https://accounts.spotify.com/api/token',
// data: querystring.stringify({
// code: code,
// redirect_uri: redirect_uri,
// grant_type: 'authorization_code'
// }),
// headers: {
// 'content-type': 'application/x-www-form-urlencoded',
// 'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64'))
// }
// });
// // Success! You now have the access token
// const { access_token, refresh_token } = response.data;
// res.send({ message: "Authenticated!", token: access_token });
// } catch (error) {
// res.send({ error: "Authentication failed", details: error.response.data });
// }
// }
// });
// // START THE SERVER
// const PORT = 8888;
// app.listen(PORT, () => {
// console.log(`🚀 App is alive at http://127.0.0.1:${PORT}/login`);
// });
// // const clientId = '3edfcb2cdb144a9796a8c39f5ce3730a';
// // const redirectUri = 'http://127.0.0.1:5500/test.html'; // Must match Spotify Dashboard!
// // function login() {
// // const scope = 'user-read-private user-read-email';
// // const authUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}`;
// // window.location.href = authUrl;
// // }
// // // Function to run on page load
// // window.onload = () => {
// // const hash = window.location.hash;
// // console.log('1')
// // console.log(hash)
// // if (hash) {
// // const token = new URLSearchParams(hash.substring(1)).get('access_token');
// // console.log(token)
// // if (token) {
// // generateContent(token);
// // }
// // }
// // };
// // async function generateContent(token) {
// // const response = await fetch('https://api.spotify.com/v1/me', {
// // headers: { 'Authorization': 'Bearer ' + token }
// // });
// // const data = await response.json();
// // document.getElementById('display').innerHTML = `<h2>Welcome, ${data.display_name}!</h2>`;
// // }
var client_id = '3edfcb2cdb144a9796a8c39f5ce3730a';
var client_secret = '2693336dd1044b1989fe26ad8f42500f';
async function getToken() {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa(`${client_id}:${client_secret}`)
},
body: 'grant_type=client_credentials'
});
const data = await response.json();
console.log(data.access_token);
return data.access_token
}
async function getplaylist(token) {
const response = await fetch('https://api.spotify.com/v1/albums/4aawyAB9vmqN3uQ7FjRGTy', {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + token
},
// body: 'grant_type=client_credentials'
});
const data = await response.json();
console.log(data.access_token);
}
async function getSpotifyAlbum(token) {
try {
const response = await fetch('https://api.spotify.com/v1/albums/4aawyAB9vmqN3uQ7FjRGTy', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Request failed:', error);
}
}
async function getUserID(token, playlistID) {
try {
const response = await fetch(`https://api.spotify.com/v1/playlists/${playlistID}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Request failed:', error);
}
}
// async function getUserID(token) {
// try {
// const response = await fetch(`https://api.spotify.com/v1/playlists/${playlistID}`, {
// method: 'GET',
// headers: {
// 'Authorization': `Bearer ${token}`
// }
// });
// if (!response.ok) {
// throw new Error(`HTTP error! status: ${response.status}`);
// }
// const data = await response.json();
// console.log(data);
// } catch (error) {
// console.error('Request failed:', error);
// return null;
// }
// }
async function getSpotifyDataV2(token) {
try {
const response = await fetch('https://api.spotify.com/v1/me/playlists', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Error ${response.status}: ${errorText}`);
}
const data = await response.json();
console.log('Success:', data);
return data;
} catch (error) {
console.error('Fetch failed:', error);
}
}
let token = await getToken()
let tempPlaylistID = '55gj1P2XPDay5TSEtSgQDu'
getUserID(token, tempPlaylistID)
getSpotifyDataV2(token);
getSpotifyAlbum(token);
// getplaylist(token)