Skip to content

Instantly share code, notes, and snippets.

@mcpeblocker
Created September 3, 2022 12:27
Show Gist options
  • Save mcpeblocker/2e3ededce9738697703036a27f4b5b46 to your computer and use it in GitHub Desktop.
Save mcpeblocker/2e3ededce9738697703036a27f4b5b46 to your computer and use it in GitHub Desktop.
Getting access_token (for Bearer auth) from spotify using your app credentials.
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}`,
},
})
}
@SaDi-BRo
Copy link

SaDi-BRo commented Sep 3, 2022

Useful gist🌚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment