【问题标题】:Using spotify-web-api-node to generate an authentication token使用 spotify-web-api-node 生成身份验证令牌
【发布时间】:2015-03-01 22:13:42
【问题描述】:

我是使用 nodejs 的新手,我正在做一个项目,我可以通过搜索一次添加一首歌曲来制作自定义播放列表。我已经能够获取代码来进行搜索并获取正确的 ID,但是当尝试添加到播放列表时,我收到一个关于范围错误的错误。长话短说,我做错了身份验证类型。

所以我阅读了 spotify-web-api-node 文档,但是在生成授权 url 和获取响应之间迷失了方向,然后通过另一种方法获取授权令牌。我不确定是否有另一种我看不到的方法可以发出请求,或者我是否应该通过普通节点方法发出常规请求。

我使用的代码几乎是来自以下链接 (https://github.com/thelinmichael/spotify-web-api-node#authorization) 的复制粘贴,其中标题为“以下使用硬编码授权代码..​​.”的第二个框就是我所在的位置丢失了...我需要从响应中获取该代码,但我不确定如何发送请求以获取响应,createAuthorizeURL 方法似乎只是创建了实际的 url 但不发送它。

【问题讨论】:

    标签: node.js spotify


    【解决方案1】:

    我相信混淆源于Authorization Code flow 的工作方式,以及我为节点包装器编写文档的方式。 createAuthorizeURL 方法的目的是帮助您创建需要将用户转发到的 URL。

    来自您链接到的同一文档:

    In order to get permissions, you need to direct the user to our Accounts service. 
    Generate the URL by using the wrapper's authorization URL method.
    

    假设用户首先进入您的网站http://www.jd.example.com。它会有一个 Spotify 风格的按钮,上面写着 Login here。该按钮链接到 createAuthorizeURL 生成的 URL。 URL 的一个非常重要的部分是 redirect_uri 查询参数。例如,您将生成的 URL 类似于

    https://accounts.spotify.com:443/authorize?client_id=5fe01282e44241328a84e7c5cc169165&
    response_type=code&redirect_uri=https://www.jd.example.com/callback&
    scope=playlist-modify-public
    

    当用户点击按钮时,他们将进入 Spotify 网站 (accounts.spotify.com/) 上的身份验证和授权流程。但是,当他们完成此流程后,Spotify 会将他们定向到您在 createAuthorizeURL 中提供的相同 redirect_uri,例如https://www.jd.example.com/callback.

    这意味着您的网络服务器(例如Express)需要能够处理对redirect_uri 的请求。如果您的网络服务器确实是 Express,它可能看起来像这样。

    /* Some express.js setup here */
    /* Some spotify-web-api-node setup here */
    
    /* Handle authorization callback from Spotify */
    app.get('/callback', function(req, res) {
    
      /* Read query parameters */
      var code  = req.query.code; // Read the authorization code from the query parameters
      var state = req.query.state; // (Optional) Read the state from the query parameter
    
      /* Get the access token! */
      spotifyApi.authorizationCodeGrant(code)
        .then(function(data) {
          console.log('The token expires in ' + data['expires_in']);
          console.log('The access token is ' + data['access_token']);
          console.log('The refresh token is ' + data['refresh_token']);
    
          /* Ok. We've got the access token!
             Save the access token for this user somewhere so that you can use it again.
             Cookie? Local storage?
          */
    
          /* Redirecting back to the main page! :-) */
          res.redirect('/');
    
        }, function(err) {
          res.status(err.code);
          res.send(err.message);
        }
      });
    });
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢迈克尔,它有很大帮助。因此,如果我正确理解这一点,我将无法加载我的程序并完全免提运行;这意味着用户可以将他们的 spotify 名称/密码存储在配置中,并且程序可以进行自己的身份验证。他们总是需要点击按钮,对吗?如果是这样的话,这不是一个交易破坏者,如果它只是一个死胡同,我只是不想继续沿着这条路跑。
    • 不,没错。这根本不是 Web API 支持的 OAuth 2.0 的工作方式。出于安全原因,需要将用户带到 Spotify 的站点进行身份验证和授权,然后将其引导回您的应用程序。但是,他们只需要授权您的应用程序一次。获得授权后,您的应用程序将收到一个有效期为一小时的访问令牌。之后,它需要刷新。由于您可以永久刷新令牌,因此用户只需完成一次登录流程。
    • 此方法是否也适用于spotify-web-api-js 节点模块?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多