Node.js nodejs spotify web api无法使用access\u令牌进行身份验证
您好,我正在尝试建立一个nodejs网站,有人可以登录到他们的spotify帐户,我可以进行api调用,如获取他们的热门曲目/获取艺术家信息/编辑播放列表等 我一直在尝试使用这个npm库: 和其他一些方法,但尚未成功地做到这一点。我正在尝试工作的工作流如下所示:Node.js nodejs spotify web api无法使用access\u令牌进行身份验证,node.js,express,npm,oauth-2.0,spotify,Node.js,Express,Npm,Oauth 2.0,Spotify,您好,我正在尝试建立一个nodejs网站,有人可以登录到他们的spotify帐户,我可以进行api调用,如获取他们的热门曲目/获取艺术家信息/编辑播放列表等 我一直在尝试使用这个npm库: 和其他一些方法,但尚未成功地做到这一点。我正在尝试工作的工作流如下所示: 用户转到http://localhost:3000/login 将您重定向到spotify登录页面的路径 用户登录后,spotify会将其重定向回http://localhost:3000/callback url将如下所示: node
function useSpotifyApi(access_token){
var SpotifyWebApi = require('spotify-web-api-node');
// credentials are optional
var spotifyApi = new SpotifyWebApi({
// clientId: 'zzz',
// clientSecret: 'zzz',
redirectUri: 'http://localhost:3000/callback'
});
spotifyApi.setAccessToken(access_token);
// Get Elvis' albums
spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE').then(
function(data) {
console.log('Artist albums', data.body);
},
function(err) {
console.error("err=",err);
}
);
}
但我总是收到一个错误,说令牌无效:
err= WebapiRegularError: An error occurred while communicating with Spotify's Web API.
Details: Invalid access token.
at _toError (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\spotify-web-api-node\src\http-manager.js:39:12)
at C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\spotify-web-api-node\src\http-manager.js:71:25
at Request.callback (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\index.js:905:3)
at C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\index.js:1127:20
at IncomingMessage.<anonymous> (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\parsers\json.js:22:7)
at Stream.emit (events.js:315:20)
at Unzip.<anonymous> (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\unzip.js:53:12)
at Unzip.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
body: { error: { status: 401, message: 'Invalid access token' } },
headers: {
'www-authenticate': 'Bearer realm="spotify", error="invalid_token", error_description="Invalid access token"',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token',
'access-control-allow-methods': 'GET, POST, OPTIONS, PUT, DELETE, PATCH',
'access-control-allow-credentials': 'true',
'access-control-max-age': '604800',
'content-type': 'application/json',
'content-encoding': 'gzip',
'strict-transport-security': 'max-age=31536000',
'x-content-type-options': 'nosniff',
date: 'Wed, 26 May 2021 01:42:45 GMT',
server: 'envoy',
via: 'HTTP/2 edgeproxy, 1.1 google',
'alt-svc': 'clear',
connection: 'close',
'transfer-encoding': 'chunked'
},
statusCode: 401
}
err=WebapiRegularError:与Spotify的Web API通信时出错。
详细信息:无效的访问令牌。
错误(C:\Users\marti\Documents\projects\nodejs express template\node\u modules\spotify web api node\src\http manager.js:39:12)
位于C:\Users\marti\Documents\projects\nodejs express template\node\u modules\spotify web api node\src\http manager.js:71:25
at Request.callback(C:\Users\marti\Documents\projects\nodejs express template\node\u modules\superagent\lib\node\index.js:905:3)
位于C:\Users\marti\Documents\projects\nodejs express template\node\u modules\superagent\lib\node\index.js:1127:20
在收到消息时。(C:\Users\marti\Documents\projects\nodejs-express-template\node\u-modules\superagent\lib\node\parsers\json.json:22:7)
在Stream.emit(events.js:315:20)
在解压时。(C:\Users\marti\Documents\projects\nodejs express template\node\u modules\superagent\lib\node\unzip.js:53:12)
在Unzip.emit(events.js:327:22)
在endReadableNT(_stream_readable.js:1220:12)
在处理和拒绝时(内部/process/task_queues.js:84:21){
正文:{错误:{状态:401,消息:'Invalid access token'},
标题:{
“www authenticate”:“Bearer realm=“spotify”,error=“invalid\u token”,error\u description=“invalid access token”,
“访问控制允许来源”:“*”,
“访问控制允许标头”:“接受、应用平台、授权、内容类型、来源、重试后、Spotify应用版本、X-Cloud-Trace-Context、客户端令牌、内容访问令牌”,
“访问控制允许方法”:“获取、发布、选项、放置、删除、修补”,
“访问控制允许凭据”:“true”,
“访问控制最大年龄”:“604800”,
“内容类型”:“应用程序/json”,
“内容编码”:“gzip”,
“严格的运输安全”:“最大年龄=31536000”,
“x-content-type-options”:“nosniff”,
日期:2021年5月26日星期三01:42:45 GMT,
服务器:'特使',
via:'HTTP/2 edgeproxy,1.1谷歌',
“alt svc”:“清除”,
连接:“关闭”,
“传输编码”:“分块”
},
状态代码:401
}
我做错什么了吗?如何使用nodejs/express通过spotify web api进行身份验证并发出api请求
err= WebapiRegularError: An error occurred while communicating with Spotify's Web API.
Details: Invalid access token.
at _toError (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\spotify-web-api-node\src\http-manager.js:39:12)
at C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\spotify-web-api-node\src\http-manager.js:71:25
at Request.callback (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\index.js:905:3)
at C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\index.js:1127:20
at IncomingMessage.<anonymous> (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\parsers\json.js:22:7)
at Stream.emit (events.js:315:20)
at Unzip.<anonymous> (C:\Users\marti\Documents\projects\nodejs-express-template\node_modules\superagent\lib\node\unzip.js:53:12)
at Unzip.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
body: { error: { status: 401, message: 'Invalid access token' } },
headers: {
'www-authenticate': 'Bearer realm="spotify", error="invalid_token", error_description="Invalid access token"',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token',
'access-control-allow-methods': 'GET, POST, OPTIONS, PUT, DELETE, PATCH',
'access-control-allow-credentials': 'true',
'access-control-max-age': '604800',
'content-type': 'application/json',
'content-encoding': 'gzip',
'strict-transport-security': 'max-age=31536000',
'x-content-type-options': 'nosniff',
date: 'Wed, 26 May 2021 01:42:45 GMT',
server: 'envoy',
via: 'HTTP/2 edgeproxy, 1.1 google',
'alt-svc': 'clear',
connection: 'close',
'transfer-encoding': 'chunked'
},
statusCode: 401
}