Skip to content

Instantly share code, notes, and snippets.

@donstefani
Created March 8, 2020 22:44
Show Gist options
  • Save donstefani/70ef1069d4eab7f2339359526563aab2 to your computer and use it in GitHub Desktop.
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
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);
}
};
@Harischand2
Copy link

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