Search code examples
jqueryajaxapiwunderlist

Wunderlist API http access via ajax to list tasks


I'm trying to access a list tasks via Wunderlist API. I failed to do it via ajax.

I can make this curl works from my terminal:

curl -H "X-Access-Token: OAUTH-TOKEN" -H "X-Client-ID: CLIENT-ID" https://a.wunderlist.com/api/v1/user

But I'm failing to do it via a ajax request:

$.ajax({
url: "https://a.wunderlist.com/api/v1/user",
beforeSend: function(xhr) { 
  xhr.setRequestHeader(
    "X-Access-Token: OAUTH-TOKEN",
    "X-Client-ID: CLIENT-ID" 
  )
},
type: 'GET',
dataType: 'json',
contentType: 'application/json',
success: function (data) {
  console.log(JSON.stringify(data));
},
error: function(){
  console.log("Cannot get data");
}
});

Anyone already did it?


Solution

  • I think your request headers are formatted incorrectly. This is the code that is working successfully for me:

    	$.ajax({
    	  type: 'GET',
    	  url: 'https://a.wunderlist.com/api/v1/user',
    	  beforeSend: function(xhr) {
    	    xhr.setRequestHeader('X-Access-Token', 'YOURACCESSTOKEN');
    	    xhr.setRequestHeader('X-Client-ID', 'YOURCLIENTID');
    	  },
    	  dataType: 'json',
    	  success: function(response, textStatus, jqXHR) {
    	    console.log(response);
    	  },
    	  error: function(jqXHR, textStatus, errorThrown) {
    	    console.log('ERROR: ' + jqXHR.status + ' - ' + jqXHR.statusText);
    	  }
    	});
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    Replace the access token and client id values with your own, and this snippet should run successfully.