如何使用 Spotify 凭据对用户进行身份验证并在 NodeJS 应用程序中获取他们的音乐偏好?

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

编辑

检查此模块以管理身份验证: https://www.npmjs.com/package/passport-spotify

有一个 NodeJs 工作代码示例。

要获取用户偏好等,请查看可与上述模块结合使用的模块: https://www.npmjs.com/package/spotify-web-api-node

查看以下示例,了解我是如何做到的:

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);

    }
    )
)   

}

这有帮助:

Passport-Spotify 使用 OAuth 2.0 API.

通过 Spotify 进行身份验证的 Passport 策略

此模块允许您在 Node.js 应用程序中使用 Spotify 进行身份验证。通过插入 Passport,Spotify 身份验证可以轻松且不显眼地集成到支持 Connect 风格中间件(包括 Express)的任何应用程序或框架中。

有关 Spotify 的 OAuth 2.0 实施的更多信息,请查看他们的 Web API 授权指南。

安装 $ npm 安装 passport-spotify

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