如何获取Spotify访问令牌-Spotify Web API节点

时间:2019-07-18 07:39:32

标签: node.js api npm authorization spotify

我引用了链接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'
});

在这种情况下,我如何获取访问令牌。.

请帮助我

1 个答案:

答案 0 :(得分:0)

上面的特定代码不会返回访问令牌。您需要参考"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是一个很好的资源,可帮助您决定要遵循的路径。