Thursday, August 20, 2009

Failed to write a twitter client bookmarklet

I saw somebody say that we could post your tweets by GET method using twitter API on the internet. This made me excited because it would mean that we could build very easy-to-use client bookmarklets. Before making sure of this information on twitter's official API guide, I started writing code like below. After a while, it turned out that twitter API allows only POST methods when we tweet. That's REST. My dream was short-lived. It's gone now. Sigh.

<html>
<head><title>Twitter Bookmark</title></head>
<body>

<script type="text/javascript">
(function() {

// This code does not work for IE6 or below.
// It is OK since IE6 has died. Good bye IE6!!
var xmlhttp = false;
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}

if(!xmlhttp) return;

// Well, you can never update any information by GET method...that's REST.
xmlhttp.open('GET', 'http://twitter.com/statuses/update.json', false, "twitter_user", "twitter_password");
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.setRequestHeader('X-Twitter-Client', 'TwitterBookmark js by Eiji Sakai');
xmlhttp.setRequestHeader('X-Twitter-Client-Version', '0.1');
xmlhttp.setRequestHeader('X-Twitter-Client-URL', 'http://elm200.blogspot.com/');
xmlhttp.send('status=' + encodeURIComponent("Hello World!"));
if(xmlhttp.status != 200) alert(xmlhttp.status + ' ' + xmlhttp.statusText);

})();

</script>

hello

</body>
</html>

No comments: