Last active
June 17, 2024 00:45
-
-
Save jordantwells42/0c34871ad3b8f832bee6d0072437f14f to your computer and use it in GitHub Desktop.
using Spotify Next Auth
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
| 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