[英]spotify-web-api-node - WebapiError: Unauthorized
var SpotifyWebApi = require('spotify-web-api-node');
var spotifyApi = new SpotifyWebApi({
clientId : 'xxx',
clientSecret : 'xxx',
redirectUri : 'https://example.com/callback'
});
spotifyApi.getTrack('2q8eudK0r9ImgCB1XhFfxG').then(function(data) {
console.log(data);
});
我的代码正常工作,但有1个月出现此错误。
(节点:12824)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):WebapiError:未经授权
(节点:12824)不建议使用警告:不建议使用的承诺拒绝已被弃用。 将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。
根据此问题,您需要设置访问令牌。
spotifyApi.setAccessToken('myAccessToken');
另外,在这里看看: https : //github.com/thelinmichael/spotify-web-api-node#setting-credentials
// Set necessary parts of the credentials on the constructor
var spotifyApi = new SpotifyWebApi({
clientId : 'myClientId',
clientSecret : 'myClientSecret'
});
// Get an access token and 'save' it using a setter
spotifyApi.clientCredentialsGrant()
.then(function(data) {
console.log('The access token is ' + data.body['access_token']);
spotifyApi.setAccessToken(data.body['access_token']);
}, function(err) {
console.log('Something went wrong!', err);
});
// Get tracks in a playlist
spotifyApi.getPlaylistTracks('thelinmichael', '3ktAYNcRHpazJ9qecm3ptn', { 'offset' : 1, 'limit' : 5, 'fields' : 'items' })
.then(function(data) {
console.log('The playlist contains these tracks', data.body);
}, function(err) {
console.log('Something went wrong!', err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.