-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathapp.js
41 lines (35 loc) · 1.14 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
/**
* This is an example of a basic node.js script that performs
* the Client Credentials oAuth2 flow to authenticate against
* the Spotify Accounts.
*
* For more information, read
* https://developer.spotify.com/documentation/web-api/tutorials/client-credentials-flow
*/
const client_id = 'YourClientId';
const client_secret = 'YourClientSecret';
async function getToken() {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
body: new URLSearchParams({
'grant_type': 'client_credentials',
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64')),
},
});
return await response.json();
}
async function getTrackInfo(access_token) {
const response = await fetch("https://api.spotify.com/v1/tracks/4cOdK2wGLETKBW3PvgPWqT", {
method: 'GET',
headers: { 'Authorization': 'Bearer ' + access_token },
});
return await response.json();
}
getToken().then(response => {
getTrackInfo(response.access_token).then(profile => {
console.log(profile)
})
});