Last active
February 12, 2021 21:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, everything is working as expected 🚀