Getting Spotify API access token from frontend JavaScript code

I believe the issue here is that you’re attempting to retrieve JSON data from the endpoint where you should direct your users. So instead of making a request to it, you should supply a button on your page that links to your https://accounts.spotify.com/authorize/{…} URL. The user will proceed to give your application the permissions you’ve requested as specified in the scope parameter, and will be directed back to the URL you’ve specified in the redirect_uri parameter. This is where you get the authorization code, which you can use in the https://accounts.spotify.com/api/token/{…} endpoint. Read more about the Authorization Code flow in the Authorization Guide.

Spotify Web API support three different oAuth flows, and you might be interested in Implicit Grant. Examples of all of these flows written in Javascript using Node is available at https://github.com/spotify/web-api-auth-examples.

Leave a Comment