Connect to Twitter from AIR example
Line Break
Author: Arjan (34 Articles) - Author Website
Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.
So everybody’s using Twitter now, so i thought it might be cool to write a small example to show you how you can connect to Twitter from an AIR application. The first thing you need is an API to connect to Twitter.
I’m using TwitterScript in my example so if you want to follow along with the example I recommend you download the *.swc file from their website. And add it to your library path by going to your project properties:
There are a couple more Twitter APIs available for ActionScript. I don’t have any experience with them yet, so I can’t tell you which one is the best at the moment.
Ok let’s make simple example that does the following:
- Let’s the user search for a Twitter user
- Get’s all the Tweets from that user and lists them in a DataGrid
- Add a text field to view the complete Tweet once a Tweet is selected in the DataGrid
- Add an image that show the Twitter users picture
- Display the Twitter users name
Searching for a Twitter user and handling the result:
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 | import mx.collections.ArrayCollection; import mx.managers.CursorManager; import twitter.api.data.TwitterUser; import twitter.api.Twitter; import twitter.api.events.TwitterEvent; import twitter.api.TwitterSearch; import twitter.api.data.TwitterStatus; [Bindable] private var twitterStatusCollection:ArrayCollection; private function doSearch():void { CursorManager.setBusyCursor(); var twit:Twitter = new Twitter(); twit.addEventListener( TwitterEvent.ON_USER_TIMELINE_RESULT, userTimelineResult); twit.loadUserTimeline(tiTwitterUser.text); } private function userTimelineResult(event:TwitterEvent):void { twitterStatusCollection = new ArrayCollection(); for each( var status:TwitterStatus in event.data ) { twitterStatusCollection.addItem(status); } CursorManager.removeBusyCursor(); } |
The layout that shows all the information:
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 | <mx:VBox width="100%" height="100%" paddingTop="50" backgroundColor="#FFFFFF" horizontalAlign="center"> <mx:HBox width="250"> <mx:Label text="Twitter user:" fontWeight="bold"/> <mx:TextInput id="tiTwitterUser" width="100%"/> </mx:HBox> <mx:HBox width="250" horizontalAlign="right"> <mx:Button label="Get Tweets!" click="doSearch()"/> </mx:HBox> <mx:VBox width="250" horizontalAlign="center"> <mx:Text text="Name: {twitterStatusCollection.length > 0 ? twitterStatusCollection.getItemAt(0).user.name : ''}" fontWeight="bold"/> <mx:Image source="{twitterStatusCollection.length > 0 ? twitterStatusCollection.getItemAt(0).user.profileImageUrl : ''}" width="150" maintainAspectRatio="true"/> <mx:DataGrid dataProvider="{twitterStatusCollection}" width="250" id="dg"> <mx:columns> <mx:DataGridColumn headerText="Tweets" dataField="text"/> </mx:columns> </mx:DataGrid> <mx:TextArea width="250" text="{dg.selectedItem.text}" height="100"/> </mx:VBox> </mx:VBox> |
Unfortunately it’s still not simple to connect to Twitter from Flex applications directly (because of restrictions on Twitters side, crossdomain.xml stuff which doesn’t allow requests from Flash movies outside of the twitter domain). It is possible though, you have can some proxy on the server (for example in PHP) to connect to Twitter and send requests and receive responses via that proxy.
The complete AIR application, however looks something like this, download it here. Don’t have the AIR runtime yet? Download it here:

Related posts:
- Twitter Search with Flex Builder
- Connect to Google Analytics from Flash Builder
- List Directory with AIR in Flex 4
Comments
5 Responses to “Connect to Twitter from AIR example”


Good Job man, the code is clean and works great! good to get started.
Thanks for sharing!
Mike
Great job dude, thanks
Hi, thanks for the code!, I´m working with Flex 3 and I have problems with the loadUserTimeline function. Flex dont´t recognize this. I´m wondering if you could know what´s the problem.
Sounds strange, the example is also in Flex 3. Are all the import statements there and is the swc file added to your library path as explained? If it is, please give me a more detailed description of the error message Flex gives you.
Very Well Done
I want to post a message on Twitter..Please help me for posting a message
Regrads
Tanzeem Akhtar Bhatti
Flex Developer