Skip to content

Instantly share code, notes, and snippets.

@joshmrallen
Created August 31, 2018 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmrallen/5aa5edea7bbbc68359ee33c851b861b4 to your computer and use it in GitHub Desktop.
Save joshmrallen/5aa5edea7bbbc68359ee33c851b861b4 to your computer and use it in GitHub Desktop.
File from Jammming project where we communicate with the Spotify API to get the access token to allow us to save the User's playlist to their Spotify account.
//variable to hold the user's access token
let accessToken = '';
//some more variables
let clientID = '';
let redirectURI = 'http://localhost:3000/';
let url = 'https://accounts.spotify.com/authorize';
//creating the Spotify module
const Spotify = {
getAccessToken() {
if (accessToken !== '') {
return accessToken;
} else {
fetch(`${url}?client_id=${clientID}&redirect_uri=${redirectURI}&response_type=token`).then(response => {
if(response.ok) {
return response.json();
}
throw new Error('Request failed!');
}, networkError => console.log(networkError.message)
).then(jsonResponse => {
//code to execute JSON response--is the response a URL with the token in it?
//If so, parse the response/url and find the token and save store it into accessToken
});
}//end if statement
} //end getAccessToken method
};//end Spotify module
export default Spotify;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment