Search code examples
javascriptoauthtumblr

Tumblr API OAuth with local test server


I'm trying to get posts from my tumblr blog and put them on a separate website page. To do this I registered an app on their OAuth page, but I'm having some issues when I try to actually request the authorization. My console spits out this message—

XMLHttpRequest cannot load https://api.tumblr.com/v2/blog/myblog.tumblr.com/posts?api_key=(MY_KEY).
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://127.0.0.1:63342' is therefore not allowed access.

(I've omitted the key value here for obvious reasons).

Now, my site isn't actually live yet, and I have a test server running at localhost:63342 but on their OAuth app settings page I have these options that I must fill out—

oauth page

Is there a way to get this to work with my local test server? Here's the code that I'm calling to request access.

var request = new XMLHttpRequest();

request.open('GET', 'https://api.tumblr.com/v2/blog/myblog.tumblr.com/posts?api_key=(API_KEY)', true);

request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
        // Success!
        var data = JSON.parse(request.responseText);
        console.log(data);
    } else {
        // We reached our target server, but it returned an error
        console.log('server error');
    }
};

request.onerror = function() {
    // There was a connection error of some sort
    console.log("ERROR!!!");
};

request.send();

Any help would be appreciated! Thanks!


Solution

  • Turn out my issue was using JSON instead of JSONP, which bypasses the Access-Control-Allow-Origin issue. I downloaded this JSONP library for Javascript ( I am not using JQuery in my project ) and was able to access the api by writing this:

    JSONP('https://api.tumblr.com/v2/blog/myblog.tumblr.com/posts?api_key=(API_KEY)'
    , function(data) {
        console.log(data);
    });
    

    Which returns a JSON Object which I can then data from using something like data.response or whatever objects are in the array.

    Again, my issue was not Tumblr not authorizing my test server. I was able to get this to work using 127.0.0.1:port as my application website & callback url.