I use the npm module spotify-web-api-node to use the Spotify Web API without having a ton of code to write.
I followed the example given here to get an Authorization Code from Spotify. Then, I use this code to get an Access Token and a Refresh Token from Spotify and perform all the actions I want.
The problem occurs when I ask the Access Token here :
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 !' })
})
})
This code logs :
Something went wrong! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }
What is wrong with my code ? What can I do to have the Access Token and Refresh Token from Spotify ? Thank you !