【问题标题】:How to get Spotify access token using Web API Node如何使用 Web API 节点获取 Spotify 访问令牌
【发布时间】:2019-07-18 07:39:32
【问题描述】:

我参考了链接https://www.npmjs.com/package/spotify-web-api-node

代码示例:

var SpotifyWebApi = require('spotify-web-api-node');

// credentials are optional
 var spotifyApi = new SpotifyWebApi({
 clientId: 'fcecfc72172e4cd267473117a17cbd4d',
 clientSecret: 'a6338157c9bb5ac9c71924cb2940e1a7',
 redirectUri: 'http://www.example.com/callback'
});

在这我怎样才能获得访问令牌?

【问题讨论】:

    标签: node.js api npm authorization spotify


    【解决方案1】:

    上面的特定代码不会返回访问令牌。您需要参考"Authorization" section of that package,您可以在其中决定授权代码流或客户端凭据流。两者都将返回一个访问令牌,但根据您的用例,您必须决定哪个最适合您。

    对于授权代码流,它看起来像这样:

    spotifyApi.authorizationCodeGrant(code).then(
      function(data) {
        console.log('The token expires in ' + data.body['expires_in']);
        console.log('The access token is ' + data.body['access_token']);
        console.log('The refresh token is ' + data.body['refresh_token']);
        ...more code
    }
    

    对于客户端凭据流,它看起来像这样:

    spotifyApi.clientCredentialsGrant().then(
      function(data) {
        console.log('The access token expires in ' + data.body['expires_in']);
        console.log('The access token is ' + data.body['access_token']);
        ...more code
    })
    

    Spotify Guide on Authorization 将是一个很好的资源,可以帮助您决定走哪条路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-09
      • 2017-02-14
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2019-08-24
      相关资源
      最近更新 更多