spotify-web-api-node
Version:
A Node.js wrapper for Spotify's Web API
52 lines (43 loc) • 2.06 kB
JavaScript
var SpotifyWebApi = require('../');
/**
* This example retrieves information about the 'current' user. The current user is the user that has
* authorized the application to access its data.
*/
/* Retrieve a code as documented here:
* https://developer.spotify.com/spotify-web-api/authorization-guide/#authorization_code_flow
*
* Codes are given for a set of scopes. For this example, the scopes are user-read-private and user-read-email.
* Scopes are documented here:
* https://developer.spotify.com/spotify-web-api/using-scopes/
*/
var authorizationCode = 'AQAgjS78s64u1axMCBCRA0cViW_ZDDU0pbgENJ_-WpZr3cEO7V5O-JELcEPU6pGLPp08SfO3dnHmu6XJikKqrU8LX9W6J11NyoaetrXtZFW-Y58UGeV69tuyybcNUS2u6eyup1EgzbTEx4LqrP_eCHsc9xHJ0JUzEhi7xcqzQG70roE4WKM_YrlDZO-e7GDRMqunS9RMoSwF_ov-gOMpvy9OMb7O58nZoc3LSEdEwoZPCLU4N4TTJ-IF6YsQRhQkEOJK';
/* Set the credentials given on Spotify's My Applications page.
* https://developer.spotify.com/my-applications
*/
var spotifyApi = new SpotifyWebApi({
clientId : 'fcecfc72172e4cd267473117a17cbd4d',
clientSecret : 'a6338157c9bb5ac9c71924cb2940e1a7',
redirectUri : 'http://www.example.com/callback'
});
// First retrieve an access token
spotifyApi.authorizationCodeGrant(authorizationCode)
.then(function(data) {
console.log('Retrieved access token', data['access_token']);
// Set the access token
spotifyApi.setAccessToken(data['access_token']);
// Use the access token to retrieve information about the user connected to it
return spotifyApi.getMe();
})
.then(function(data) {
// "Retrieved data for Faruk Sahin"
console.log('Retrieved data for ' + data['display_name']);
// "Email is farukemresahin@gmail.com"
console.log('Email is ' + data.email);
// "Image URL is http://media.giphy.com/media/Aab07O5PYOmQ/giphy.gif"
console.log('Image URL is ' + data.images[0].url);
// "This user has a premium account"
console.log('This user has a ' + data.product + ' account');
})
.catch(function(err) {
console.log('Something went wrong', err);
});