0

I'm new to JSON requests, cURL, etc. and I can't figure out how to correctly format a JSON request with authorization. I'm trying to call on a Spotify API and they reccomend a cURL request:

GET https://api.spotify.com/v1/search

and

curl -X "GET" "https://api.spotify.com/v1/search?q=tania%20bowra&type=artist" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ReallyLongKey"

I don't know how to use cURL and GET so I tried to use ajax:

function Search() {
      $.ajax({
        url: 'https://api.spotify.com/v1/search?q=Muse&type=track%2Cartist&market=US&limit=10&offset=5',
        datatype: 'json',
        type: 'get',
        cache: false,
        success: function(Data) {
            //stuff
        }
    });
}

How do I use cURL or add authentication to the ajax method? Thanks in advance, Justin.

Justin Luce
  • 25
  • 2
  • 6
  • 1
    use `headers` field in `ajax` request? https://stackoverflow.com/questions/41929654/how-to-add-header-to-request-in-jquery-ajax – muradm Nov 08 '18 at 19:11
  • I tried that, got this error. SpotifyTest.html:1 Access to XMLHttpRequest at 'https://api.spotify.com/v1/search?q=Muse&type=track%2Cartist&market=US&limit=10&offset=5&_=1541708047402' from origin 'null' has been blocked by CORS policy: Request header field X-AUTH-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response. Although that's unrelated to the Auth-Token I think. – Justin Luce Nov 08 '18 at 20:15
  • Did you check documentation? https://developer.spotify.com/ – muradm Nov 08 '18 at 20:18
  • Yeah, they only go over this method: curl -X "GET" "https://api.spotify.com/v1/search?q=tania%20bowra&type=artist" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ReallyLongKey" Thanks for the quick response! – Justin Luce Nov 08 '18 at 20:19
  • Did you create your application in spotify developer? Did you authenticate and acquire the key? Make sure that you are familiar with this: https://developer.spotify.com/documentation/general/guides/authorization-guide/, it is not just simple `curl` request. – muradm Nov 08 '18 at 20:26

0 Answers0