node.js – Authorizing against Spotify with Node
I’ve been trying to get a token from the Spotify API using NodeJs (within the Nuxt). I keep getting a 400 bad request response though. I’ve asked both ChatGPT and Claude but they couldn’t help me either. See the code below thats causing the error, the useRuntimeConfig() function loads the appropriate .env variables.
The response body as it appears in the console is: ERROR [nuxt] [request error] [unhandled] [400] Request failed with status code 400
import axios from 'axios';
import qs from 'querystring';
export default defineEventHandler(async (e) => {
const config = useRuntimeConfig();
const response = await axios.post('https://accounts.spotify.com/api/token',
qs.stringify({
grant_type: 'client_credentials',
client_id: config.public.spotifyClientId,
client_secret: config.spotifyClientSecret
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
});
Read more here: Source link
