You're experiencing a 401 error when calling the Spotify API, specifically the /me/player/currently-playing endpoint. This typically indicates an authentication or permissions issue.
-
Verify Environment Variables:
# Check if your .env file contains: VITE_CLIENT_ID=your_actual_spotify_client_id -
Restart Development Server:
npm run dev
Environment variables require a restart to take effect.
- Go to Spotify Developer Dashboard
- Select your app
- Verify settings:
- Redirect URIs: Must include
http://localhost:5173/ - App Type: Should be Web App
- Save any changes
- Redirect URIs: Must include
The auth debugger has been added to your Dashboard. Use it to:
- Click "Show Debug Info" - Check console for token status
- Click "Test Profile API" - Test basic API access
- Click "Test Currently Playing" - Test the specific endpoint
- Click "Re-authenticate" - Get fresh tokens with all scopes
- Symptom: Had working tokens, now getting 401
- Solution: Click "Refresh Token" in debugger or re-authenticate
- Symptom: Some endpoints work, others return 401
- Solution: Re-authenticate to get all required scopes
- Required Scopes:
user-read-currently-playing user-read-playback-state user-modify-playback-state
- Symptom: 204 "No Content" response
- Solution: Start playing music on any Spotify client:
- Spotify Desktop App
- Spotify Mobile App
- Spotify Web Player
- Must have Spotify Premium
- Symptom: Authentication redirects but no tokens received
- Solution: Double-check Client ID in
.envfile
Open browser console and run:
// Check stored tokens
console.log("Access Token:", localStorage.getItem("access_token"));
console.log("Refresh Token:", localStorage.getItem("refresh_token"));
console.log("Expires:", localStorage.getItem("expires"));
// Test API manually
fetch("https://api.spotify.com/v1/me", {
headers: {
Authorization: "Bearer " + localStorage.getItem("access_token"),
},
})
.then((r) => r.json())
.then(console.log);// Test the specific endpoint that's failing
fetch("https://api.spotify.com/v1/me/player/currently-playing", {
headers: {
Authorization: "Bearer " + localStorage.getItem("access_token"),
},
}).then(async (response) => {
console.log("Status:", response.status);
if (response.status === 204) {
console.log("No track currently playing");
} else {
const data = await response.json();
console.log("Response:", data);
}
});Important: Spotify Connect APIs (playback control) require:
- ✅ Spotify Premium subscription
- ✅ Active playback on any device
- ✅ Device available in Spotify Connect
To test without Premium:
- Use search, browse, and library endpoints
- View track/artist/album information
- Access user profile data
If running on different port or domain:
- Update redirect URI in Spotify app settings
- Update
redirectUrlinauth.service.ts - Clear browser cache and localStorage
- Tokens automatically refresh when expired
- 401 errors trigger refresh attempt
- Failed refresh redirects to login
- AuthDebugger component added to Dashboard
- Console logging for token operations
- Direct API testing buttons
- Axios interceptors handle 401s
- Detailed error logging
- Graceful fallbacks
- Use the Auth Debugger in your Dashboard
- Re-authenticate to ensure fresh tokens
- Check Console for detailed error messages
- Test with Premium Account for full functionality
- Start Spotify Playback before testing player endpoints
- The
/me/player/currently-playingendpoint returns 204 (No Content) when nothing is playing - This is not an error - it's the expected response when no track is active
- Only Premium users can use playback control endpoints
- Free users can still access search, browse, and library data
The AuthDebugger component is now available in your Dashboard to help identify and resolve authentication issues!