Created
September 2, 2012 08:25
-
-
Save chestozo/3595862 to your computer and use it in GitHub Desktop.
Tumblr API usage with OAuth: creating a text post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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