Search code examples
node.jsspotify

How to authenticate a user with their Spotify credentials and get their music preferences in a NodeJS-app?


EDIT

Check this module for managing the authentication: https://www.npmjs.com/package/passport-spotify

There is an example with working code for NodeJs to start from.

To get user preferences, etc. check out this module which can be combined with the one above: https://www.npmjs.com/package/spotify-web-api-node

Check out the following example on how I have done it:

module.exports = function(passport) {
passport.use(
    new SpotifyStrategy(
      {
        clientID: '2f0be1f441d641bc8bcc960de9789196',
        clientSecret: '9088b47ae05241748ceae01d06871265',
        callbackURL: 'http://localhost:8888/auth/spotify/callback',
      },
      async (accessToken, refreshToken, expires_in, profile, done) => {
        
        // Initialize spotifyapi object
        var spotifyApi = new SpotifyWebApi({
            clientID: '2f0be1f441d641bc8bcc960de9789196',
            clientSecret: '9088b47ae05241748ceae01d06871265',
            callbackURL: 'http://localhost:8888/auth/spotify/callback',
        });

        // Set accesstoken for api objct
        spotifyApi.setAccessToken(accessToken);
    
        return done(null, profile);

    }
    )
)   

}


Solution

  • This helped:

    Passport-Spotify Passport strategy for authenticating with Spotify using the OAuth 2.0 API.

    This module lets you authenticate using Spotify in your Node.js applications. By plugging into Passport, Spotify authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

    For more information about Spotify's OAuth 2.0 implementation, check their Web API Authorization Guide.

    Installation $ npm install passport-spotify

    http://www.passportjs.org/packages/passport-spotify/