UNPKG

spotify-web-api-node

Version:
55 lines (45 loc) 1.9 kB
const 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/documentation/general/guides/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/documentation/general/guides/scopes/ */ const authorizationCode = '<insert authorization code with user-read-private and user-read-email scopes>'; /* Get the credentials from Spotify's Dashboard page. * https://developer.spotify.com/dashboard/applications */ const spotifyApi = new SpotifyWebApi({ clientId: '<insert client id>', clientSecret: '<insert client secret>', redirectUri: '<insert redirect URI>' }); // First retrieve an access token spotifyApi .authorizationCodeGrant(authorizationCode) .then(function(data) { console.log('Retrieved access token', data.body['access_token']); // Set the access token spotifyApi.setAccessToken(data.body['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.body['display_name']); // "Email is farukemresahin@gmail.com" console.log('Email is ' + data.body.email); // "Image URL is http://media.giphy.com/media/Aab07O5PYOmQ/giphy.gif" console.log('Image URL is ' + data.body.images[0].url); // "This user has a premium account" console.log('This user has a ' + data.body.product + ' account'); }) .catch(function(err) { console.log('Something went wrong:', err.message); });