I often find myself battling with OAuth and the Twitter API when I have a requirement to post to twitter from an external site or web app. So using basic jQuery I have found a simple to use solution which also avoids all of the usual user steps needed when authenticating with Twitter.
<textarea id="tweetstatus" rows="4" cols="50"> Write something... </textarea> <a href="" class="tweet">Tweet</a> <script type="text/javascript"> $("a.tweet").live("click", function() { var tweetStatus = $('textarea#tweetstatus').val(); var encodedTweet = htmlEscape(encodeURI(tweetStatus)+ ""); var shareURL = "&url=http://www.example.com" var fullURL = 'http://twitter.com/share?text=' + encodedTweet + shareURL; $(this).attr("href", fullURL) function htmlEscape(str) { return String(str) .replace(/#/g, '%23') } return true; }) </script>
This simple methods simply updated the href of the share button with the contents of the textarea and then posts to Twitter using the twitter.com/…URL.
There are 4 variable to consider: tweetStatus = The value of the textarea encodedTweet = The URL encoded ‘tweetStatus’ (an additioanl function ‘htmlEscape’ is required to encode #HashTags) shareURL = Any link you wish to share in addition to your status update fullURL = The fully encoded URL
This give the user a simple, real time experience of updating there Twitter status without the hastle of verifying new apps.
Please comment if you have any better alternatives or improvements.
Posted By
admin
Categories
Uncategorized