Skip to content

Instantly share code, notes, and snippets.

@colormono
Created December 4, 2018 14:17
Show Gist options
  • Save colormono/e6a4a1090359077d7c29890cf8e84d61 to your computer and use it in GitHub Desktop.
Save colormono/e6a4a1090359077d7c29890cf8e84d61 to your computer and use it in GitHub Desktop.
Get Spotify API Token with Javascript
import axios from 'axios';
import qs from 'qs';
// your application requests authorization
var CLIENT_ID = 'your-app-id';
var CLIENT_SECRET = 'your-app-secret';
var accessToken = '';
axios({
method: 'post',
baseURL: 'https://accounts.spotify.com/api/token',
data: qs.stringify({ grant_type: 'client_credentials' }),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${new Buffer(
CLIENT_ID + ':' + CLIENT_SECRET
).toString('base64')}`
}
})
.then(function(response) {
console.log(response);
// save the token
accessToken = response.data.access_token;
})
.catch(function(error) {
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment