Your Spotify integration now has enhanced authentication with robust error handling and debugging tools.
useAuthTokenHook: Advanced token management with automatic refresh- Enhanced AuthDebugger: Comprehensive authentication testing
- SpotifyApiTest Component: Real-time API status monitoring
- Automatic Token Refresh: Handles expired tokens transparently
- Detailed Error Logging: Console logs for debugging authentication issues
The dashboard now shows two debugging components:
- 🐛 Auth Debugger: Test authentication and tokens
- 🎵 Spotify API Test: Monitor API calls in real-time
Look at the Auth Debugger panel:
- ✅ Authenticated: Should show "Yes"
- ✅ Token Available: Should show "Yes"
- ✅ Token Valid: Should show "Yes"
- ✅ Refresh Token: Should show "Available"
Click the buttons in Auth Debugger to test:
- Tests
/meendpoint - Should return user profile data
- If 401: Token/permissions issue
- Tests
/me/playerendpoint (your failing endpoint) - Should return playback state or 204
- If 401: Missing playback permissions
- Tests
/me/player/currently-playingendpoint - Should return current track or 204
- If 401: Missing playback permissions
Symptoms:
- Had working API calls before
- Now getting 401 on all endpoints
- Token shows as expired
Solution:
// Click "Force Refresh" button or
// Click "Re-authenticate" to get fresh tokensSymptoms:
- Some API calls work (like profile)
- Player/playback endpoints return 401
- Fresh tokens don't help
Solution:
- Click "Re-authenticate" button
- Make sure to grant all permissions in Spotify dialog
- Check console for scope information
Required Scopes for Player APIs:
user-read-playback-state
user-modify-playback-state
user-read-currently-playing
Symptoms:
- Authentication redirect works
- But no tokens received after callback
- 401 on all API calls
Solution:
- Verify
.envfile has correctVITE_CLIENT_ID - Restart dev server:
npm run dev - Re-authenticate
Symptoms:
- Random 401 errors
- Authentication sometimes works
Solution:
- Go to Spotify Developer Dashboard
- Check your app settings:
- Redirect URIs: Must include
http://localhost:5174/(or whatever port Vite is using) - App Type: Web Application
- Redirect URIs: Must include
- Save changes and re-authenticate
Open browser console and run these tests:
console.log("=== TOKEN INSPECTION ===");
console.log("Access Token:", localStorage.getItem("access_token"));
console.log("Refresh Token:", localStorage.getItem("refresh_token"));
console.log("Expires:", localStorage.getItem("expires"));
console.log("Current Time:", new Date().toISOString());const token = localStorage.getItem("access_token");
fetch("https://api.spotify.com/v1/me", {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
console.log("Profile API Status:", response.status);
return response.json();
})
.then((data) => console.log("Profile Data:", data))
.catch((error) => console.error("Profile Error:", error));const token = localStorage.getItem("access_token");
fetch("https://api.spotify.com/v1/me/player", {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
console.log("Player API Status:", response.status);
if (response.status === 204) {
console.log("No active device - this is normal");
return null;
}
return response.json();
})
.then((data) => console.log("Player Data:", data))
.catch((error) => console.error("Player Error:", error));- 200 + Data: ✅ Authentication working
- 401: ❌ Token/authentication issue
- 200 + Data: ✅ Active playback found
- 204 No Content: ✅ No active device (normal)
- 401: ❌ Missing playback permissions
- 403: ❌ Non-premium account
- 200 + Data: ✅ Track currently playing
- 204 No Content: ✅ Nothing playing (normal)
- 401: ❌ Missing permissions
- ✅ Check .env file: Correct
VITE_CLIENT_ID - ✅ Restart server:
npm run dev - ✅ Re-authenticate: Click "Re-authenticate" button
- ✅ Grant all permissions: Don't skip any scopes in Spotify dialog
- ✅ Test endpoints: Use debugging buttons
- ✅ Check console: Look for detailed error logs
- ✅ Verify premium: Player APIs require Spotify Premium
- Open browser DevTools → Network tab
- Click "Test Player API" button
- Look for the API request:
- Request Headers: Should include
Authorization: Bearer ... - Response: Note status code and response body
- Request Headers: Should include
The new useAuthToken hook includes:
- Automatic expiration checking
- Background token refresh
- Error state management
- Real-time validation
Enhanced logging shows:
- Token operations: Refresh attempts, errors
- API calls: Request/response details
- Authentication flow: Step-by-step process
- Error context: Detailed error information
For /me/player endpoints to work:
- ✅ Spotify Premium Account: Required for playbook control
- ✅ Active Spotify Client: One of these must be running:
- Spotify Desktop App
- Spotify Mobile App
- Spotify Web Player (open.spotify.com)
- ✅ Device Available: Must appear in Spotify Connect devices list
Free accounts can use:
- Search endpoints
- User profile
- Library management (saved tracks/albums)
- Browse/recommendations
If you're still getting 401 errors after following this guide:
- Clear all data: Logout → Clear browser cache → Re-authenticate
- Check Spotify status: Spotify Status Page
- Verify app settings: Double-check Spotify Developer Dashboard
- Use debugging tools: The enhanced debugger provides detailed logs
The new implementation includes comprehensive error handling and should provide clear feedback about what's causing the 401 error!