Skip to content

Instantly share code, notes, and snippets.

@Uvacoder
Forked from jordantwells42/Spotify.ts
Created June 17, 2024 00:45
Show Gist options
  • Select an option

  • Save Uvacoder/af6d9dd2b222f4248dc7f8c129ad7300 to your computer and use it in GitHub Desktop.

Select an option

Save Uvacoder/af6d9dd2b222f4248dc7f8c129ad7300 to your computer and use it in GitHub Desktop.
using Spotify Next Auth
const client_id = process.env.SPOTIFY_CLIENT_ID
const client_secret = process.env.SPOTIFY_CLIENT_SECRET
const authorization_code = Buffer.from(`${client_id}:${client_secret}`).toString('base64')
const getAccessToken = async (refresh_token: string) => {
const response = await fetch(`https://accounts.spotify.com/api/token`, {
method: 'POST',
headers: {
Authorization: authorization_code,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token
})
})
return response.json()
}
export const getUsersPlaylists = async (refresh_token: string) => {
const { access_token } = await getAccessToken(refresh_token)
return fetch('https://api.spotify.com/v1/me/playlists', {
headers: {
Authorization: `Bearer ${access_token}`
}
})
}
export const getSearch = async (refresh_token: string, query: string) => {
const { access_token } = await getAccessToken(refresh_token)
const querystring = new URLSearchParams({
q: query,
type: 'track',
limit: '5',
market: 'US'
}).toString()
return fetch(SEARCH_ENDPOINT + '?' + querystring, {
headers: {
Authorization: `Bearer ${access_token}`
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment