authorization/client_credentials/app.js (27 lines of code) (raw):
/**
* 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)
})
});