Created
March 8, 2020 22:44
-
-
Save donstefani/70ef1069d4eab7f2339359526563aab2 to your computer and use it in GitHub Desktop.
Getting an access token for a React app from the Spotify API using Node.js and Axios
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
import axios from 'axios'; | |
import qs from 'qs'; | |
export const getAuth = async () => { | |
const clientId = process.env.REACT_APP_BASIC_CLIENT_ID; | |
const clientSecret = process.env.REACT_APP_BASIC_CLIENT_SECRET; | |
const headers = { | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
auth: { | |
username: clientId, | |
password: clientSecret, | |
}, | |
}; | |
const data = { | |
grant_type: 'client_credentials', | |
}; | |
try { | |
const response = await axios.post( | |
'https://accounts.spotify.com/api/token', | |
qs.stringify(data), | |
headers | |
); | |
console.log(response.data.access_token); | |
return response.data.access_token; | |
} catch (error) { | |
console.log(error); | |
} | |
}; |
Thanks, the code all the way to the top still works for 2024! Happy Debugging!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you ! saved me big time