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