Skip to content

Instantly share code, notes, and snippets.

@chestozo
Created September 2, 2012 08:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chestozo/3595862 to your computer and use it in GitHub Desktop.
Save chestozo/3595862 to your computer and use it in GitHub Desktop.
Tumblr API usage with OAuth: creating a text post
var oauth = ChromeExOAuth.initBackgroundPage({
'request_url': 'http://www.tumblr.com/oauth/request_token',
'authorize_url': 'http://www.tumblr.com/oauth/authorize',
'access_url': 'http://www.tumblr.com/oauth/access_token',
'consumer_key': 'vGKikr2uBRsWoDZ947UjvSzA58HYEXDNjAer7VEXnLXlQt4Ozi',
'consumer_secret': 'JvofKKxQ2QSiVXVaWMBZQRzC5cI86YpJzAoouIkUdlvSeYXN2e',
'app_name': 'Photo post adder'
});
function stringify(parameters) {
var params = [];
for(var p in parameters) {
params.push(encodeURIComponent(p) + '=' +
encodeURIComponent(parameters[p]));
}
return params.join('&');
};
function done() {
// Just output it.
console.log(arguments);
};
function onAuthorized() {
var method = 'POST';
var url = 'http://api.tumblr.com/v2/blog/chestozo.tumblr.com/post';
var data = {
type: 'text',
tags: 'text,my first post',
title: 'First attempt',
body: 'That\'s all folks!'
};
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
done(xhr, data);
};
xhr.open(method, url, true);
xhr.setRequestHeader('Authorization', oauth.getAuthorizationHeader(url, method, data));
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(stringify(data));
};
oauth.authorize(onAuthorized);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment