spotify-web-api-node: authorizationCodeGrant() 给出 400 - 错误请求
spotify-web-api-node: authorizationCodeGrant() give a 400 - Bad Request
我使用 npm 模块 spotify-web-api-node
来使用 Spotify Web API 而无需编写大量代码。
我按照 here 给出的示例从 Spotify 获得了 授权码 。然后,我使用此 code 从 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 获得访问令牌和刷新令牌?谢谢!
问题很简单(我浪费了2天时间...)。如 Spotify API 文档 here 中所述。
谈到 redirect_uri
参数,文档说:
TLDR 阅读此内容:
The value of this parameter must exactly match the value of redirect_uri supplied when requesting the authorization code.
所以在我的代码中:
router.get('/auth/spotify/success', (req, res, next) => {
let spotifyApi = new SpotifyWebApi({
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
redirectUri: 'http://localhost:3000/'
// Changing this...
redirectUri: 'http://localhost:3000/auth/spotify/success'
// ...to this made it work !
})
// [...]
我也很抱歉,因为没有人能找到问题,因为我没有给你完整的代码 "the first part works !"。是的,它有效,但它包含有关我的问题的有用线索。
我使用 npm 模块 spotify-web-api-node
来使用 Spotify Web API 而无需编写大量代码。
我按照 here 给出的示例从 Spotify 获得了 授权码 。然后,我使用此 code 从 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 获得访问令牌和刷新令牌?谢谢!
问题很简单(我浪费了2天时间...)。如 Spotify API 文档 here 中所述。
谈到 redirect_uri
参数,文档说:
TLDR 阅读此内容:
The value of this parameter must exactly match the value of redirect_uri supplied when requesting the authorization code.
所以在我的代码中:
router.get('/auth/spotify/success', (req, res, next) => {
let spotifyApi = new SpotifyWebApi({
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
redirectUri: 'http://localhost:3000/'
// Changing this...
redirectUri: 'http://localhost:3000/auth/spotify/success'
// ...to this made it work !
})
// [...]
我也很抱歉,因为没有人能找到问题,因为我没有给你完整的代码 "the first part works !"。是的,它有效,但它包含有关我的问题的有用线索。