SWX Twitter API For Dummies

I’ve always had a problem understanding documentation for APIs, hence when I do get something, I have this urge to share so that anyone who has the same problem can do what I am trying to do.

In my opinion, the best documentation ever written is by gskinner for TweenMax, and I think few would argue with me about that.

Having said that, the documentation written by aral for the SWX Twitter API is actually not too bad as well. But being completely new to both the Twitter API and the SWX Twitter API, one might be lost upon landing on the API page.

Now that my ramblings are done, on with the tutorial.

First, download the SWX Code from here. As of the time of writing, go ahead and download the v2 which is still in BETA as it works just fine.

This tutorial will show you how easy it is to update your Twitter status using the SWX Twitter API.

Here’s a look at what we will be creating:
[kml_flashembed publishmethod="static" fversion="8.0.0" movie="http://www.designfission.com/swxtutorial/twitter.swf" width="400" height="380" targetclass="flashmovie"]

Get Adobe Flash player

[/kml_flashembed]

Code:

stop();
loader.serviceClass = "Twitter";
loader.method = "getPublicUpdates";
loader.debug = true;
loader.loadMovie("http://swxformat.org/php/swx.php","GET");

import org.swxformat.*;
var swx:SWX = new SWX();
swx.gateway = "http://swxformat.org/php/swx.php";
swx.encoding = "POST";
swx.timeout = 10;
swx.debug = true;
swx.faultHandler = Delegate.create(this, faultHandler);
swx.progressHandler = Delegate.create(this, progressHandler);
swx.timeoutHandler = Delegate.create(this, timeoutHandler);

function updateTwitter() {

	var callParameters:Object = {
		serviceClass:"Twitter",
		method:"update",
		args:[message_txt.text, username_txt.text, password_txt.text], 

		result: [this, resultHandler],
		progress: [this, progressHandler],
		timeout: [this, timeoutHandler],
		fault: [this, faultHandler],
		debug: true
	}
	swx.call(callParameters);
	isLoading(true);
}

function resultHandler(event:Object) {
	isLoading(false);
	status.text = event.result;
}

function progressHandler(event:Object) {
	trace("progressHandler: " + event.bytesLoaded + " of " + event.bytesTotal);
}
function timeoutHandler(event:Object) {
	isLoading(false);
	status.text = "Call timeout. Please try again.";
}
function faultHandler(event:Object) {
	isLoading(false);
	status.text = "Call fault. Please try again.";
}

update_btn.onRelease = function() {
	updateTwitter();
};

Source Files (AS2.0 | CS3 Format)

I’m not going to explain the code unless someone requests for it as it’s really quite simple and straightforward. The other reason is due to a lack of time. I figured if I don’t share this now, I might not want to do it in the future, so hey, at least you have the code. :D

Once again, hope this helps someone out there.

Note: For other Flash Developers like me out there, if you need to integrate Facebook as well using AS2.0, take a look at http://www.echoesofburmajones.com/2009/09/18/20/facebook_connect_in_flash_actionscript_2.htm for an excellent tutorial (w/source files)!

If this post helped you in any way, help me in return by clicking on the ad below:

6 Responses to “SWX Twitter API For Dummies”

  1. Aaron

    Does this still work? I tried it and it didn’t post to my twitter account…

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>