Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spotify Whitelisted URI still returns { "error": "invalid_grant", "error_description": "Invalid redirect URI" }

I know there are already questions similar to this but all of the answers are mostly "Oh I forgot to put the slash at the end" But this is absolutely driving me crazy. Im trying to get an access token from Spotify API but i keep getting the invalid redirect uri error.

Here is my api call

const request = require('superagent');

const data = {
    grant_type: 'authorization_code',
    code: code,
   // redirect_uri: encodeURIComponent('http://localhost:3000/Test')
   redirect_uri: 'http://localhost:3000/Test'
};

request.post('https://accounts.spotify.com/api/token')
    .set({
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' + base64.encode(configs.client_id + ':' + configs.client_secret)
    })
    .send(data)
    .end((err, tokenRes) => {
        if (tokenRes) {
            res.send({token: tokenRes})
        } else {
            res.error(err);
        }
    });

and these are the URIs I have whitelisted:

http://localhost:3000/LoginRedirect

http://localhost:3000/Test

http://localhost:3000/Home

I added so many combinations to the whitelist with slashes at the end, http:// s removed searched for wildcards but i cant get rid of this error... Any help is appreciated.

like image 603
let_the_coding_begin Avatar asked May 05 '18 16:05

let_the_coding_begin


1 Answers

Ran into the same problem myself after some refactoring, and was going insane myself. The redirect_uri in the post request has to be the same as the first redirect_uri from the client side. From the Spotify docs:

Required. This parameter is used for validation only (there is no actual redirection). The value of this parameter must exactly match the value of redirect_uri supplied when requesting the authorization code.

like image 150
Justin Chan Avatar answered Oct 18 '22 07:10

Justin Chan