import axios from 'axios'; export default async function handler(req, res) { const { code } = req.query; const { clientId, clientSecret, redirectUri } = req.body; if (!code) { return res.status(400).json({ error: 'No code provided' }); } try { const response = await axios.post( 'https://accounts.spotify.com/api/token', new URLSearchParams({ grant_type: 'authorization_code', code: code, redirect_uri: redirectUri, client_id: clientId, client_secret: clientSecret, }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, } ); res.status(200).json(response.data); } catch (error) { console.error('Token exchange error:', error.response?.data || error.message); res.status(500).json({ error: 'Failed to exchange token', details: error.response?.data }); } }