Error gives 'invalid client' and Bad Request. Cross-checked headers, can't find the cause of the error. Encountered this while implementing Spotify's Authorization Code Flow.
The header of this post must contain Authorization as a parameter
Base 64 encoded string that contains the client ID and client secret key. The field must have the format: Authorization:
<base64 encoded client_id:client_secret>
app.get('/callback', (req, res) => {
const code = req.query.code || null;
const state = req.query.state || null;
const storedState = req.cookies ? req.cookies[stateKey] : null;
axios({
method: 'post',
url: 'https://accounts.spotify.com/api/token',
data: queryString.stringify({
grant_type: 'authorization_code',
code: code,
redirect_uri: REDIRECT_URI
}),
headers: {
'content-type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${new Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`.toString('base64'))}`
},
json: true
})
.then(response => {
if (response.status === 200) {
const { access_token, token_type } = response.data;
console.log(response.data)
axios.get('https://api.spotify.com/v1/me', {
headers: {
Authorization: `${token_type} ${access_token}`
}
})
.then(response => {
res.send(`<pre>${JSON.stringify(response.data, null, 2)}</pre>`);
})
.catch(error => {
res.send(error)
})
} else { res.send(response) }
})
.catch(error => {
// res.send(err)
if (error.response) {
console.log(error.response.data)
res.send(error)
}
});
})