Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
pu098 committed Feb 1, 2024
1 parent 6c183a7 commit 2ca86b2
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/beatbuddy/src/spotify/getSong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ let playbackPosition: number | undefined;

const tracksWithoutPreview: Set<string> = 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)) {
Expand All @@ -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: {
Expand Down

0 comments on commit 2ca86b2

Please sign in to comment.