Twitter in Flash, A Starter Kit / Thursday, July 17, 2008


Source

First, the source: Twitter Badge Source Code.


What Is It?

An overly simple Flash-based Twitter badge that displays some specified user’s last twitter post. This was made to be a tutorial for you to play with.

Edit 7-18-08: Twitter approved my whitelisting request which is awesome. It hasn’t taken effect yet, but that means soon no “Exceeded 100 requests” messages.

The Steps to Make It

  • Download source (above)
  • Install proxy.php (included in source) on your server by uploading it to a php-friendly host
  • Go to the address to installed the proxy.php script. Do you see a blank page or an curl error?
  • If you see a blank page, you are good; if you see a curl error, install proxy.php on some other server you have access to. If you use some other server, make sure you put a crossdomain.xml onto this other server’s base directory (i.e. http://www.mydomain.com/crossdomain.xml). An example crossdomain.xml file is supplied with the source, you will need to edit the url inside that file to fit your own domain name.
  • Modify “Twitter.as” (in source code) so that PROXY_SERVER_ADDRESS variable is equal to your proxy’s address (proxy address is from previous step)
  • Enjoy and expand the functionality to your content. I don’t provide Twitter API or AS3 support, however you can do what I did: search for experts’ advice.

If You Are Interested, The Process...

First thing I did was innocently search for the ActionScript 3 (AS3) libraries which would let me connect to the Twitter API. Twitter has let a Google Group take over the Flash library development and where to find the latest Flash library is listed here: List of Twitter API Libraries.

I downloaded the Twitter API Library for AS3 by opening up the Terminal (OS X) and inputting the command described here: Twitterscript: How to check out the needed as3 classes

Once I downloaded the AS3 classes and after some confusion, I basically looked at the Twitter.as methods, and did the simplest possible sequence of commands to get started (see the zipped code above).

Second Hurdle: Security Error

So things were working great when I tested my app locally, but then I posted the app online *blammo* big security error. Well, I knew Flash was a pain in the ass for things like this, so I didn’t get too discouraged. I just did a quick search for PHP proxies after being tipped off by Ed Finkler (see “Thanks” section below) that Flash needs a PHP proxy when we’re pulling in information from outside sources who don’t know us. In this case we’re pulling in XML data from Twitter. So we’re going to need a proxy.

The proxy I used I included in the source set (see above).

But my host doesn’t support Curl, so I got this error when the proxy tried to call curl_init(). So I did some quick searches and decided I’d put the proxy.php script on another site (that I have access to) which does support Curl. So I did this and I got the same error. Then I noticed the browser was looking for crossdomain.xml on this other host I was using. So I modified a crossdomain.xml file sitting around on my computer to allow this domain to get xml data from the proxy.php script. I included this crossdomain.xml file in my source (see above).

And… after all of that… it worked! But… of course there is a catch… you can only see my tweets occasionally because of this awful 100 request per hour limit. So I need to add some stuff to my own Flash file to catch this quota error… but I’m okay with that, since I did what I set out to do - make the damn badge.

Thanks

Okay it was misery trying to get this simple example to work, I’ll be honest.

Thanks Ed Finkler, Chris Scott, and everyone over at Twitter Development Talk.