Thursday, August 28, 2008

Search Twitter from Flash

I have updated the Twitter ActionScript API. I added support for search. You are probably aware that search is provided by Summize, who was acquired by Twitter. It is pretty obvious that the APIs have not yet been merged!

Twitter's API is all based on Rails ActiveResource ... which is awesome. It turns any resource (often a database table) into a RESTful service. REST is often associated with XML, but Rails (and thus Twitter) supports it as JSON (Twitter supports ATOM and RSS as well) too. For ActionScript, XML is great. Or I should say POX is great and that is what Rails serves up.

The Twitter Search API is different. It supports two formats: ATOM and JSON. No POX. I went with the ATOM format. For JSON, I would have used Adobe's corelib. It works well, but I didn't want to add the weight. Plus, JSON tends to parse much slower in AS3 than XML. That is because AS3 implements E4X. To get E4X to work with ATOM, you have to be mindful of namespaces. For example, here is the code I used to interate over the entries: for each (entryXml in xml.atom::entry). Here the xml variable is the parsed result from and atom is a Namespace object. Not as pretty as just xml.entry, but oh well.


Tony said...

Peter Armstrong (of Flexible Rails fame) forked your google code version to github here:
for use in a book he's working on.

I forked Peter's version here: and added back in the authentication piece.

In Flash versions later than 9.0.115 you can do authentication headers if the site's crossdomain accepts them. Twitter's crossdomain does not (we should lobby to get that changed), but the authentication still works fine for AIR, so the authentication is still useful.

Just letting you know. Thanks for the work on the code.

Michael Galpin said...

Great information. I merged authorization back in to TwitterScript. In the hope of Twitter setting the crossdomain.xml to be more friendly, I did the base64 encoding sans mx package, so the library could be used by non-Flex apps.

Kunal said...

Hi Michael, great work on TwitterScript. I really appreciate that you added a TwitterSearch class, but I'm having some trouble implementing it. When I try to run anything using twitterscript, I get an Output Error generated from -

Location:, Line 281
Description: 1046: Type was not found or was not a compile-time constant: TwitterSearch.

I'm pretty new to AS3, so I'm not sure how to fix this problem. Basically I want to just call a search for a keyword and parse through the results to output in a text field. Any help or advice you could offer would be fantastic. A sample working search file would really really help me with my grad school project if you have the time, I would really appreciate it. Thanks!


Kunal said...

Michael, after some further investigation (re: staying up all night) I got the Search bit working, totally my own fault for not changing class paths and just learning how to work with. However! I now have some new questions heh -

1) It seems that the dates parsed in the atom feed from the Search are not in the same format as other dates that are returned, so that if I go try to trace the Status.createdAt property, I get "Invalid Date".
2) This might be an Atom issue as well from parsing the data to XML, but it doesn't seem like there is a Status.user property that results from the search? Any time I try and trace Status.user I get no data (blank) I can't access or anything else.

Do you have any thoughts about these 2 issues? Am I going about accessing 'createdAt' and 'user' in the wrong way? If it would be easier, I can send you the code I'm working on, I really would appreciate any help you can offer when you have the time.


Peter Gardner said...

Is there any explicit documentation for the TwitterScript code?


Jim Robb said...


Complete noob here. I work at a school that has a digital screen (firesign) that I can upload a fla file to. I wish to have a twitter search box display so students can tweet messages, and the sign will display the search. The twitter account is USsign. I have no clue how to do it, but I imagine it is possible to pull the search into the fla and display it.

Any thoughts? twitter robbjim