【发布时间】:2018-06-07 14:43:39
【问题描述】:
我使用 npm 模块 spotify-web-api-node 来使用 Spotify Web API,而无需编写大量代码。
我按照here 给出的示例从 Spotify 获取授权码。然后,我使用此代码从 Spotify 获取 Access Token 和 Refresh Token 并执行我想要的所有操作。
当我在这里询问 Access Token 时出现问题:
router.get('/auth/spotify/success', (req, res, next) => {
let spotifyApi = new SpotifyWebApi({
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
redirectUri: 'http://localhost:3000/'
// The URI is registered to Spotify redirect URIs
})
const code = req.query.code
spotifyApi.authorizationCodeGrant(code)
.then(data => {
console.log('The token expires in ' + data.body['expires_in'])
console.log('The access token is ' + data.body['access_token'])
console.log('The refresh token is ' + data.body['refresh_token'])
// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body['access_token'])
spotifyApi.setRefreshToken(data.body['refresh_token'])
res.render('index', { title: 'Connected !' })
})
.catch(err => {
console.log('Something went wrong!', err);
res.render('index', { title: 'Error !' })
})
})
此代码记录:
Something went wrong! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }
我的代码有什么问题?我该怎么做才能从 Spotify 获得访问令牌和刷新令牌?谢谢!
【问题讨论】:
-
我对 Spotify API 一无所知,但您的 redirectUri 似乎很可疑。不应该是公共网址吗?
-
好吧,当我使用 localhost 请求授权码时,它可以完美运行
-
我用公共域试过了,不行
-
所以你的端点'/auth/spotify/success'被调用,你的代码变量被正确初始化并且spotifyApi.authorizationCodeGrant调用失败,对吧?
-
没错。我尝试了很多事情,我真的需要通过这种方式进行身份验证才能执行我想要的操作。
标签: node.js express oauth spotify