spotify-web-api-node - WebapiError:未经授权
Posted
技术标签:
【中文标题】spotify-web-api-node - WebapiError:未经授权【英文标题】:spotify-web-api-node - WebapiError: Unauthorized 【发布时间】:2017-11-09 04:36:56 【问题描述】: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 个月后出现此错误。
(node:12824) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝(拒绝ID:1):WebapiError:未经授权
(节点:12824) DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。
【问题讨论】:
看这里github.com/thelinmichael/spotify-web-api-node/issues/86 我已经重新生成了客户端ID和秘密ID... 【参考方案1】:根据this issue需要设置访问令牌。
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);
);
【讨论】:
谢谢!在此期间我已经找到了解决方案。以上是关于spotify-web-api-node - WebapiError:未经授权的主要内容,如果未能解决你的问题,请参考以下文章
spotify-web-api-node:authorizationCodeGrant()给出400 - 错误请求
使用 spotify-web-api-node 生成身份验证令牌
Sometimes we can pretend we have never been to some places.