Twitter AS3 library TwitterScript Flex Example

I have been looking around for Actionscript 3 libraries for the retrieval of data via the very nice Twitter API. Basically, TwitterScript seems to be a good choice for a Twitter Flash library as it wraps Twitter query URLs and packs the XML results automatically in arrays.

Twitter Banner

However, as so often with Actionscript libraries there is NO documentation pertaining TwitterScript available on its Google code page, there is no example included in the source code and the source code is not commented. What could be so difficult about providing a little piece of code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
      addedToStage="init()">
  <mx:Script>
    <![CDATA[
      import twitter.api.data.TwitterStatus;
      import twitter.api.events.TwitterEvent;
      import twitter.api.TwitterSearch;
        import twitter.api.Twitter;
 
      private static const TWITTER_USER:String = "stephenfry";
 
      private var _twitter:Twitter;
      private var _twitterSearch:TwitterSearch;
 
      private function init():void{
        _twitter = new Twitter();
        _twitterSearch = new TwitterSearch();
 
        _twitter.loadUserTimeline(TWITTER_USER);
        _twitter.addEventListener(TwitterEvent.ON_USER_TIMELINE_RESULT, 
            userTimelineResult);
 
        _twitterSearch.addKeyword("ipad");
        _twitter.addEventListener(TwitterEvent.ON_SEARCH, onSearchResult, 
            false,0,true);
        _twitter.search(_twitterSearch);
      }
 
      private function userTimelineResult(e:TwitterEvent):void {
        var twitterStatus:TwitterStatus;
        for (var i in e.data) {
          twitterStatus = e.data[i]
          trace("user tweets",twitterStatus.text)
        }
      }
 
      private function onSearchResult(event : TwitterEvent) : void{
        var twitterStatus : TwitterStatus;
        for (var i : int = 0; i < event.data.length; i++){
          twitterStatus = event.data[i];
          trace("search result:",twitterStatus.text);
        }
      }
    ]]>
  </mx:Script>
</mx:Application>

This example originates to Trevor Boyle’s blog. Improved by Kimi.

Posted in Actionscript, Adobe Air, Flash, flex, Twitter Tagged with: Actionsc, Adobe Air, Flash, flex, Twitter
  • Thank you very much, this was very helpful!

  • Pingback: Creating a Twitter Widget Using Flex and TwitterScript | The Blog of Jon Karna()

  • Pingback: Twitter Widget using Flex 3 Revisited | The Blog of Jon Karna()

  • hemanth

    where do i get the document for the apis of twitter script

  • Actually, as I said in my text above, there is no twitter script documentation.

    if you are looking however for the documentation of the twitter API you will find it here: http://apiwiki.twitter.com/

  • Thanks Much. True, my documentation search was not fruitful.

  • Abhi

    Hi,

    I m new to FLEX

    I tried this code but I got an error “Type was not found or was not a compile time constant: TwitterEvent”

    and

    A file found in a source-path must have the same package structure ”, as the
    definition’s package, ‘com.oreilly.aircookbook.ch16’. twitter/src

    TwitterUserVO.as Unknown 1260790250093 385

    Please help me out.

    Thanks.

  • you have to put the twitterscript swc on your classpath: http://twitterscript.googlecode.com/files/TwitterApi.1.3.swc

    i don’t know where this comes from:
    “com.oreilly.aircookbook.ch16”

  • Thanks for sharing!
    There is realy no documentation out there, so i am using your code to start with the twitterscript api.
    But i have one question: why do you create _twitterSearch? you never use it. Did you try using _twitterSearch?

    Thanks!

  • i found out using the _twitterSearch

    _twitter = new Twitter();
    _twitterSearch = new TwitterSearch();
    _twitterSearch.addKeyword(“apple”);
    _twitter.addEventListener(TwitterEvent.ON_SEARCH, onSearchResult, false,0,true);
    _twitter.search(_twitterSearch);

    private function onSearchResult(event : TwitterEvent) : void
    {
    var twitterStatus : TwitterStatus;
    for (var i : int = 0; i < event.data.length; i++)
    {
    twitterStatus = event.data[i];
    trace(twitterStatus.text);
    }
    }

  • hi kimi,
    thank you for sharing your knowledge! i will update the entry soon.

    you were completely right: i never used _twitterSearch in my example code.

    cheers,
    johannes

  • Ben

    Thanx for this.

    Has anyone trying to Authorize a user?

    I tried doing this _twitter.setAuthenticationCredentials(“myusername”, “myPass”);

    But I recieve an error and read on many blogs and on Adobes website that you cant user – http://kb.adobe.com/selfserviceviewContent.do?externalId=kb403184. but has anyone found a work around for this?

  • Nico

    Hi,

    1 question:

    I tried to use the “Search”… The results are coming in, but their “TwitterUser” Objects are blank ?!

    Have i to atuh myself first or something like that? šŸ™

  • Mike

    There are other options out there now, Tweetr is a good one. Lots of documentation, can be used with pure AS3
    http://tweetr.swfjunkie.com/

  • I am getting this error when I try to run the search function –
    ReferenceError: Error #1069: Property length not found on com.twitter.api.data.TwitterSearchData and there is no default value.

    Any ideas??

  • Hi, you saved my life!

    Ran perfectly, i just use the “fromUser” to filter my search and finish!

    Thanks for your example.

    Best Regards.

  • Albert Frostgena

    I got the same problem when I try to search anything they throw this error that property lenght not found. What I solve that problem? I’m trying to search anything and got some problems.

    Thanks in advance!

  • Neil

    I tested in Flex 3 and it works fine, but when I try to use it in Flex 4 I get two errors, Doesn’t recognise these two functions:

    loadUserTimeline
    search

    I guess its something to do with importing of the swc, not sure any pointers anyone? I put it all in

    Thanks

  • M.USAMA

    I’m trying to use Flash CS4/AS3 to pull in the latest Twitter entries for a user, and it works fine when i run it locally, but i receive this error when it’s on a server:

    TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::[email protected] to flash.events.IOErrorEvent.
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/redirectEvent()

    Has anyone run into this before? Any ideas? I’m also open to other Twitter API’s seeing as there isn’t really an official one anymore. Thanks in advance!