Access-Control-Allow-Origin denied spotify api [duplicate]

Solution 1:

The request to https://accounts.spotify.com/api/token needs to be made server side and not as an AJAX request.

This way your key, which contains the credentials for your application, won't be exposed. Also, the Spotify server will be able to redirect the request to the redirect_uri together with the access token.

An alternative is to use the implicit grant flow where you can run everything client side, but you will not get a refresh token.

I would recommend you to review the Spotify Web API Authorization Guide, check the GitHub repo with auth examples and take a look at the libraries and wrappers that make it easier to implement the OAuth flow.