1
votes

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 !

1
I know nothing about Spotify API, but your redirectUri seems suspicious to be. Shouldn't it be a public url? - Bence Gedai
Well, localhost works perfectly when I use it asking for the Authorization Code - user7383906
I tried with a public domain, doesn't work - user7383906
So your endpoint '/auth/spotify/success' gets called, your code variable is initialised properly and the spotifyApi.authorizationCodeGrant call fails, right? - Bence Gedai
Exactly. I tried many things and I really need to authenticate this way to perfom the actions I want. - user7383906

1 Answers

3
votes

The problem was simple (I wasted 2 days on it...). As specified in the Spotify API documentation here. Speaking about the redirect_uri parameter the documentation says :

TLDR READ THIS :

The value of this parameter must exactly match the value of redirect_uri supplied when requesting the authorization code.

So in my 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 !
    })

    // [...]

I also apologize because nobody could have found the problem because I didn't give you the entire code saying "the first part works !". Yes, it works but it contains useful clues about my problem.