debug: expose Spotify token error details

This commit is contained in:
ssbg04 committed 2026-07-07 21:43:33 +08:00
1 parent 7af8df289a
commit 70b627a2e4
1 file changed
+48 -26
+48 -26
View File
@@ -5,6 +5,15 @@ const client_id = import.meta.env.SPOTIFY_CLIENT_ID || process.env.SPOTIFY_CLIEN
const client_secret = import.meta.env.SPOTIFY_CLIENT_SECRET || process.env.SPOTIFY_CLIENT_SECRET || ''; const client_secret = import.meta.env.SPOTIFY_CLIENT_SECRET || process.env.SPOTIFY_CLIENT_SECRET || '';
const refresh_token = import.meta.env.SPOTIFY_REFRESH_TOKEN || process.env.SPOTIFY_REFRESH_TOKEN || ''; const refresh_token = import.meta.env.SPOTIFY_REFRESH_TOKEN || process.env.SPOTIFY_REFRESH_TOKEN || '';
console.log('Spotify Config Diagnostic:', {
clientIdPresent: !!client_id,
clientIdLength: client_id.length,
clientSecretPresent: !!client_secret,
clientSecretLength: client_secret.length,
refreshTokenPresent: !!refresh_token,
refreshTokenLength: refresh_token.length,
});
const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64'); const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64');
const NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`; const NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`;
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`; const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
@@ -12,43 +21,56 @@ const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
// Helper to retrieve a fresh Spotify access token using the refresh token // Helper to retrieve a fresh Spotify access token using the refresh token
async function getAccessToken() { async function getAccessToken() {
if (!client_id || !client_secret || !refresh_token) { if (!client_id || !client_secret || !refresh_token) {
console.error('Spotify API keys are missing in env.'); throw new Error('Spotify API keys are missing in env.');
return null;
} }
try { const response = await fetch(TOKEN_ENDPOINT, {
const response = await fetch(TOKEN_ENDPOINT, { method: 'POST',
method: 'POST', headers: {
headers: { Authorization: `Basic ${basic}`,
Authorization: `Basic ${basic}`, 'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/x-www-form-urlencoded', },
}, body: new URLSearchParams({
body: new URLSearchParams({ grant_type: 'refresh_token',
grant_type: 'refresh_token', refresh_token,
refresh_token, }),
}), });
});
if (!response.ok) { if (!response.ok) {
console.error('Failed to fetch Spotify access token:', response.statusText); const errText = await response.text();
return null; throw new Error(`Spotify token error: ${response.status} - ${errText}`);
}
const data = await response.json();
return data.access_token;
} catch (error) {
console.error('Error fetching Spotify access token:', error);
return null;
} }
const data = await response.json();
return data.access_token;
} }
// Server-side GET API handler // Server-side GET API handler
export const GET: APIRoute = async () => { export const GET: APIRoute = async () => {
const token = await getAccessToken(); let token = null;
let tokenError = null;
try {
token = await getAccessToken();
} catch (error: any) {
tokenError = error.message || String(error);
}
if (!token) { if (!token) {
return new Response( return new Response(
JSON.stringify({ isPlaying: false, message: 'Spotify integration not configured or credentials missing.' }), JSON.stringify({
isPlaying: false,
message: 'Spotify integration not configured or credentials missing.',
debug: {
clientIdPresent: !!client_id,
clientIdLength: client_id.length,
clientSecretPresent: !!client_secret,
clientSecretLength: client_secret.length,
refreshTokenPresent: !!refresh_token,
refreshTokenLength: refresh_token.length,
tokenError
}
}),
{ {
status: 200, status: 200,
headers: { headers: {