debug: expose Spotify token error details
This commit is contained in:
1 parent
7af8df289a
commit
70b627a2e4
1 file changed
+48
-26
@@ -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 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 NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`;
|
||||
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
|
||||
async function getAccessToken() {
|
||||
if (!client_id || !client_secret || !refresh_token) {
|
||||
console.error('Spotify API keys are missing in env.');
|
||||
return null;
|
||||
throw new Error('Spotify API keys are missing in env.');
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(TOKEN_ENDPOINT, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Basic ${basic}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token,
|
||||
}),
|
||||
});
|
||||
const response = await fetch(TOKEN_ENDPOINT, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Basic ${basic}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('Failed to fetch Spotify access token:', response.statusText);
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
} catch (error) {
|
||||
console.error('Error fetching Spotify access token:', error);
|
||||
return null;
|
||||
if (!response.ok) {
|
||||
const errText = await response.text();
|
||||
throw new Error(`Spotify token error: ${response.status} - ${errText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
}
|
||||
|
||||
// Server-side GET API handler
|
||||
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) {
|
||||
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,
|
||||
headers: {
|
||||
|
||||
Reference in new issue
Block a user