Created
September 3, 2022 12:27
-
-
Save mcpeblocker/2e3ededce9738697703036a27f4b5b46 to your computer and use it in GitHub Desktop.
Getting access_token (for Bearer auth) from spotify using your app credentials.
This file contains 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 = "YOUR_CLIENT_ID" | |
const client_secret = "YOUR_CLIENT_SECRET" | |
const refresh_token = "YOUR_REFRESH_TOKEN" | |
const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64') | |
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token` | |
const getAccessToken = async () => { | |
const response = await fetch(TOKEN_ENDPOINT, { | |
method: 'POST', | |
headers: { | |
Authorization: `Basic ${basic}`, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
body: new URLSearchParams({ | |
grant_type: 'refresh_token', | |
refresh_token, | |
}), | |
}) | |
return response.json() | |
} | |
export const getSpotifyInfo = async (endpoint) => { | |
const { access_token } = await getAccessToken() | |
return fetch(endpoint, { | |
headers: { | |
Authorization: `Bearer ${access_token}`, | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful gist🌚