Skip to content

Instantly share code, notes, and snippets.

@owalid
Last active February 12, 2021 21:48
Show Gist options
  • Save owalid/cf7a2425d9733b6aee80832f6fbc660c to your computer and use it in GitHub Desktop.
Save owalid/cf7a2425d9733b6aee80832f6fbc660c to your computer and use it in GitHub Desktop.
Get Spotify Access Token (client credentials) with nodejs (axios and https) without client
import axios from 'axios'
const auth = `Basic ${Buffer.from(`CLIENT_ID:SECRET`).toString('base64')}`;
const config = {
method: 'post',
url: `https://accounts.spotify.com/api/token`,
headers: {
'Authorization': auth,
'Accept':'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: "grant_type=client_credentials"
};
const result = await axios(config);
console.log(result.data);
import https from 'https'
const auth = `Basic ${Buffer.from(`CLIENT_ID:SECRET`).toString('base64')}`;
const options = {
hostname:'accounts.spotify.com',
path:'/api/token',
method: 'POST',
headers:{
'Authorization': auth,
'Accept':'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
};
const r = https.request(options, (res) => {
console.log(res.statusCode);
res.on('data', (c) => {
console.log(JSON.parse(c));
});
});
r.write("grant_type=client_credentials");
r.end();
@runmaxde
Copy link

Thank you, everything is working as expected 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment