Access-Control-Allow-Origin denied api definition

I am trying to access Spotify API tokens like this:

$.ajax({
  url: "https://accounts.spotify.com/api/token",
  type: 'POST',
  contentType: "application/json; charset=\"utf-8\"",
  crossDomain: true,
  data: {
    grant_type: "authorization_code",
    code: code,
    redirect_uri: "http://www.bancadigital.com.br/spotifyteste/callback.html"
  },
  processData: false,
  dataType: "json",
  headers: {
    Authorization: "Basic " + utf8_to_b64(key)
  },
  success: function( response ) {
    alert(response.access_token);
  },
});
Run codeHide result

but the service returns the following error:

XMLHttpRequest cannot load https://accounts.spotify.com/api/token . The requested resource does not have an Access-Control-Allow-Origin header. The origin of http://www.bancadigital.com.br 'is therefore not permitted.

Does anyone know how I can access the service?

+4
source share
1 answer

The request https://accounts.spotify.com/api/tokenshould be executed on the server side, and not as an AJAX request.

, key, , . , Spotify redirect_uri .

, , .

-API Spotify, GitHub auth , OAuth.

+10

All Articles