从Spotify Api获取信息时遇到问题

时间:2020-06-04 22:28:42

标签: node.js api spotify

从SpotifyApi检索信息时出现401错误。即使我已经保存了访问令牌。以下是我的代码段。

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

    // Save the access token so that it's used in future calls
    spotifyApi.setAccessToken(data.body['access_token']);
  }, function(err) {
    console.log('Something went wrong when retrieving an access token', err.message);
  });

spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE').then(
    function(data) {
      console.log('Artist albums', data.body);
    },
    function(err) {
      console.error(err);
    }
  );

我的终端输出是

[Error [WebapiError]: Unauthorized] { statusCode: 401 }
The access token expires in 3600
The access token is BQC5dKw5ArfFn7t-NgutgOOCRXoiWLvP6i9N0sUQj4RdaTsQYvZXN26kMU47EfS2jM4Lo1rwHdLLovo1aag

但是,如果我将.getArtist()移到.clientCredentialsGrant()

,则代码运行良好
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']);

    // Save the access token so that it's used in future calls
    spotifyApi.setAccessToken(data.body['access_token']);

    spotifyApi.getArtist('2hazSY4Ef3aB9ATXW7F5w3')
    .then(function(data) {
      console.log('Artist information', data.body);
    }, function(err) {
      console.error(err);
    });

  }, function(err) {
    console.log('Something went wrong when retrieving an access token', err.message);
  });

0 个答案:

没有答案