Search code examples
typescriptangulartumblr

Get posts from my Tumblr API


I need to get my tumblr posts over an api.

I've managed to setup the api key. I'm using Angular2, typescript for this.

I'm using jsonp so that I dont get a cross origin issue.

This is my current attempt:

  var config = {
    params: {
      action: "query",
      prop: "revisions",
      format: "json",
      callback: "JSON_CALLBACK"
    }
  }

  var url = "https://api.tumblr.com/v2/blog/thepoolcover.tumblr.com/posts?api_key=lqoK2G4BT9X8xnewyW0l45ky4aTKWTqQ3PzF14gefIglpIRnBz";

  this.jsonp.request(url, config).subscribe(response => {

      console.log(response);

  }); 

I dont get a typescript compiler error, however, I do get a browser exception console error:

enter image description here


Solution

  • You shoud use JSONP_CALLBACK for the name of the callback instead of JSON_CALLBACK and URLSeachParams for the query parameters:

    URLSearchParams search = new URLSearchParams();
    search.set('action', 'query');
    search.set('prop', 'revisions');
    search.set('format', 'json');
    search.set('callback', 'JSONP_CALLBACK');
    
    var config = {
      search: search
    };
    
    var url = "https://api.tumblr.com/v2/blog/thepoolcover.tumblr.com/posts?api_key=lqoK2G4BT9X8xnewyW0l45ky4aTKWTqQ3PzF14gefIglpIRnBz";
    
    jsonp.request(url, config).subscribe(response => {
      console.log(response);
    }); 
    

    Here is the corresponding plunkr: