diff --git a/src/beatbuddy/src/spotify/getSong.ts b/src/beatbuddy/src/spotify/getSong.ts index 4ee78eb3..094429f5 100644 --- a/src/beatbuddy/src/spotify/getSong.ts +++ b/src/beatbuddy/src/spotify/getSong.ts @@ -8,6 +8,25 @@ let playbackPosition: number | undefined; const tracksWithoutPreview: Set = new Set(); + +async function refreshSpotifyAccessToken() { + const clientId = process.env.REACT_APP_SPOTIFY_CLIENT_ID; + const clientSecret = process.env.REACT_APP_SPOTIFY_CLIENT_SECRET; + const encodedCredentials = Buffer.from(`${clientId}:${clientSecret}`).toString('base64'); + + const tokenResponse = await fetch('https://accounts.spotify.com/api/token', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Authorization': `Basic ${encodedCredentials}` + }, + body: 'grant_type=client_credentials' + }); + + const tokenData = await tokenResponse.json(); + return tokenData.access_token; +} + export async function playSong(trackId: string) { try { if (tracksWithoutPreview.has(trackId)) { @@ -17,9 +36,6 @@ export async function playSong(trackId: string) { const spotifyClient: SpotifyWebApi = await getSpotifyClient(); - const accessToken = spotifyClient.getAccessToken(); - console.log('Access Token playSong:', accessToken); - // Retrieve the preview URL for the track const response = await fetch(`https://api.spotify.com/v1/tracks/${trackId}`, { headers: {