Created
February 27, 2015 10:08
-
-
Save JMPerez/79850d6ae05e5715ba87 to your computer and use it in GitHub Desktop.
Web API Support
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
var refreshToken = 'xxxxxxxxxx'; // obtained using Authorization Code flow | |
var spotifyApi = new SpotifyWebApi({ | |
scope : ['user-read-private', 'user-read-email', 'playlist-modify-public'], | |
clientId : 'xxxxxxxxxx', | |
clientSecret : 'xxxxxxxxxxxxxxx' | |
}); | |
// we set the refresh token that will be used to obtain a fresh access token | |
spotifyApi.setRefreshToken(refreshToken); | |
// we refresh the access token | |
spotifyApi.refreshAccessToken() | |
.then(function(data) { | |
spotifyApi.setAccessToken(data['access_token']); | |
// we call the API | |
spotifyApi.createPlaylist('MYuserID', 'My Cool Playlist', { 'public' : true }) | |
.then(function(data) { | |
console.log("Created a new playlist"); | |
}, function(err) { | |
console.log('Something went wrong with creating playlist!', err); | |
}); | |
}); | |
// NOTE: This is a very naive approach. Ideally, the token should not be refreshed every | |
// time there is a call to the API. In order to to this properly, the status code from the | |
// request to the API should be checked, so if it is 401 (Unauthorized), the token should | |
// be refreshed and, if a new access token is obtened, the initial request should be retried | |
// This seems to be using the Spotify Web API Node wrapper. Keep an eye on | |
// https://github.com/thelinmichael/spotify-web-api-node/pull/25 which will expose status codes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment