• Home
  • About
  • Publications
  • Contact
  • Imprint
  •  

    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.

    16 Responses to “Twitter AS3 library TwitterScript Flex Example”

    1. Peter says:

      Thank you very much, this was very helpful!

    2. [...] google code, you can find TwitterScript.  My initial widget was based on this article, Twitter AS3 library TwitterScript Flex Example.  To find out more user feedback, I was lead to this article, Twitter and Flash (twitterscript). [...]

    3. [...] google code, you can find TwitterScript.  My initial widget was based on this article, Twitter AS3 library TwitterScript Flex Example.  To find out more user feedback, I was lead to this article, Twitter and Flash (twitterscript). [...]

    4. hemanth says:

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

    5. johannes says:

      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/

    6. Brian Sobel says:

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

    7. Abhi says:

      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.

    8. johannes says:

      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″

    9. Kimi says:

      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!

    10. Kimi says:

      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);
      }
      }

    11. johannes says:

      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

    12. Ben says:

      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?

    13. Nico says:

      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? :(

    14. Mike says:

      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/

    15. Greg says:

      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??

    16. Hi, you saved my life!

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

      Thanks for your example.

      Best Regards.

    Leave a Reply