-
-
Save Uvacoder/03bd2b422987804793ffafb91a736e16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const refreshTokens = async () => { | |
| try { | |
| const credentials = await getSpotifyCredentials() //we wrote this function above | |
| const credsB64 = btoa(`${credentials.clientId}:${credentials.clientSecret}`); | |
| const refreshToken = await getUserData('refreshToken'); | |
| const response = await fetch('https://accounts.spotify.com/api/token', { | |
| method: 'POST', | |
| headers: { | |
| Authorization: `Basic ${credsB64}`, | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| }, | |
| body: `grant_type=refresh_token&refresh_token=${refreshToken}`, | |
| }); | |
| const responseJson = await response.json(); | |
| if (responseJson.error) { | |
| await getTokens(); | |
| } else { | |
| const { | |
| access_token: newAccessToken, | |
| refresh_token: newRefreshToken, | |
| expires_in: expiresIn, | |
| } = responseJson; | |
| const expirationTime = new Date().getTime() + expiresIn * 1000; | |
| await setUserData('accessToken', newAccessToken); | |
| if (newRefreshToken) { | |
| await setUserData('refreshToken', newRefreshToken); | |
| } | |
| await setUserData('expirationTime', expirationTime); | |
| } catch (err) { | |
| console.error(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment