Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding tags to tumblr post via API

I'm trying to add tags to a tumblr post with jquery, via the tumblr api.

So far I've called the api as so:

$.ajax({
    url: 'http://api.tumblr.com/v2/blog/<My Blog>.tumblr.com/post/edit',
    method: 'post',
    data : ({
        api_key :'<My Secret Key>',
    }),
    dataType: 'jsonp',
    success: function(results){

    }
})

I've found the location of the tags via get. They're located at

results.response.posts[#].tags[#]

I've never done an api post before though so I'm not sure what to do at the success function. Any help would be appreciated.

like image 350
Matt Coady Avatar asked Oct 20 '22 16:10

Matt Coady


1 Answers

You're right you do need to use OAuth to make the request, however there are Javascript Clients (including one for jQuery) which will help you to do this.

Before proceeding though you will need to register your application in order to get the OAuth keys.

The jQuery specific OAuth client can be found here and the function below is adapted from it's documentation.

Information on the other Javascript clients can be found at the OAuth page.

    function tagEdit(tagID, tags){

var oauth = OAuth({
    consumer: {
        public: '[public key]',
        secret: '[secret key]'
    },
    signature_method: 'HMAC-SHA1'
});


var request_data = {
    url: 'http://api.tumblr.com/v2/blog/[your blog].tumblr.com/post/edit',
    method: 'POST',
    data: {
        id: tagID, tag: tags
    }
};


var token = {
    public: '[public token]',
    secret: '[public key]'
};


$.ajax({
    url: request_data.url,
    type: request_data.method,
    data: request_data.data,
    headers: oauth.toHeader(oauth.authorize(request_data, token))
}).done(function(data) {
    //what happens after the post has taken place
});
}

NB none of this has been tested as I don't have a tumblr account and I don't fancy registering for OAuth keys. Hope this might be of some use to you though.

like image 62
joesch Avatar answered Oct 23 '22 23:10

joesch