The new SWX Twitter API (and how to build your first Flash Twitter mashup in four lines of code!)

Twitter released an update to the official Twitter API this month and I've updated the SWX Twitter API so that it now has all the latest official Twitter API methods as well as some new custom ones.

Try it out!

You can try out the SWX Twitter API methods online right this moment by using the SWX Service Explorer.

If you want to develop Flash (and Flash Lite) applications that use data from Twitter, you can get started without installing anything at all by using the public SWX gateway on this site. (The public SWX gateway is located at http://swxformat.org/php/swx.php).

In fact, you can create your very first Twitter mashup in just four lines of code by following these simple instructions:

  1. Open the SWX Data Analyzer
  2. In Flash, create a new FLA.
  3. Create a new movie clip and give it the instance name loader.
  4. On the frame that has the loader movie clip, add the following script:
    loader.serviceClass = "Twitter";
    loader.method = "getPublicUpdates";
    loader.debug = true;
    loader.loadMovie("http://swxformat.org/php/swx.php", "GET");

That's all you need to get the latest public timeline updates from Twitter into Flash (you can see that the data is being loaded if you look in the SWX Data Analyzer).

To display the updates in Flash, trace out the value of loader.result.

Note: The Flash IDE will give you a security sandbox warning but the application will run correctly (look in the SWX Data Analyzer or trace out the loaded data to check this.) This warning occurs because you are running the SWF in the Flash IDE. If you put the SWF file on the same domain as the SWX gateway, it will also work without requiring any further code. However, if you want to use the Public SWX gateway and deploy your Flash applications to your own server, you must either manually call System.security.allowDomain, or (the recommended way is to) use the SWX Full API, as exlained below.

Notice that you didn't need to download or install anything. That's because SWX is native! It uses SWF files to store and exchange data.

That's all you need to start working with SWX and Twitter! No API, external classes, etc. are necessary! And that little snippet of code (which, by the way, fits on to a moo card) also shows you exactly how SWX works. It loads the data in a SWF file and the data is accessible the moment it loads as native Flash objects. No deserialization necessary.

Of course, if you want to host your own SWX gateway or develop and test offline on your own machine, you can download and install SWX to your own machine. That will also allow you to create your own service classes and APIs.

Using the SWX Full API

If you don't want to work manually with movie clips (and that is understandable), you can use the SWX Full API -- a completely abstract (and recommended) way of working with SWX.

Here's how to display the text of the last status update of your friends using the SWX Full API:

import org.swxformat.*;
 
var swx:SWX = new SWX();
swx.gateway = "http://swxformat.org/php/swx.php";
swx.encoding = "POST";
swx.debug = true; 
 
var callParameters:Object =
{
    serviceClass: "Twitter",
    method: "getNumFriendsUpdates",
    args: ["aral", 1],
    result: [this, resultHandler]
}
 
swx.call(callParameters);
 
function resultHandler(event:Object)
{
    // Display the text property of the first result.
    trace (event.result[0].text);
}

I hope you enjoy working with the SWX Twitter API (also check out the Flickr API while you're at it) and, again, please feel free to hit the public SWX gateway here on swxformat.org.

New Twitter API method reference

The newly added official Twitter API methods are listed below for your convenience. You can find (and test) the full list of methods in the SWX Service Explorer.

Status methods

  • replies
  • destroy

User methods

  • featured (was fixed in the official API and so I've re-enabled it)

Direct message methods

  • sentDirectMessages
  • newDirectMessage
  • destroyDirectMessage

Friendship methods

  • friendshipCreate
  • friendshipDestroy
  • verifyCredentials
  • endSession

New custom methods

Status methods

  • getPublicUpdates (alias for getNumPublicTimelineUpdates)

Friendship methods

  • friendsNoAuth
  • followersWhoAreNotFriends
  • getFans (alias for followersWhoAreNotFriends)
  • notifications

Deprecated methods

The following methods are deprecated as of the upcoming Beta 1.4 release in favor of the new official API methods that replace them. Please do not rely on these methods in the future as they may be removed from the API completely later on.

  • addFriend (use friendshipCreate instead)
  • removeFriend (use friendshipDestroy instead)

Post Metadata

Date
July 28th, 2007

Author
Aral

Tags


8 Trackbacks & Pingbacks

  1. March 3, 2009 4:31 am

    50 Công cụ Twitter hay dành cho Designers và Developers | Giải Pháp Số :

  2. March 3, 2009 9:42 am

    50 Twitter Tools and Tutorials For Designers and Developers :

  3. March 3, 2009 1:57 pm

    50 Twitter Tools and Tutorials For Designers and Developers | ClickLogin Web Design :

  4. March 6, 2009 7:26 am

    50 Twitter Tools and Tutorials For Designers and Developers | The Scripts Zone :

  5. April 13, 2009 7:28 pm

    The new SWX Twitter API at ting :

  6. April 29, 2009 11:03 am

    Development « iDAT 203 - Negotiated Project :

  7. May 4, 2009 2:07 am

    All Twitter Tools & Tutorials 4 Designers & Developers | Twitter Social Networking :

  8. June 6, 2009 9:54 am

    50 Twitter Tools and Tutorials For Designers and Developers | Tasarım,Bilim,Müzik,Kitap,Sanat,Weblog :

22 Comments


  1. Mo Shahin

    I’m new with SWX, But have some how good experience with flash, I came across twitter and found that there is a way to integrated with flash, I’ve tried to do the simple tutorial they have online but it didn’t work
    Can you give me some hint on how to start playing with this amazing tool



  2. mo

    I tried the above Code, without installing anything on my machine
    1- just opened flash
    2- created movieclip loader
    3- pasted your code on the first frame
    4- run and get that error

    *** Security Sandbox Violation ***
    SecurityDomain ‘http://swxformat.org/php/swx.php?serviceClass=Twitter&method=getPublicUpdates&debug=true’ tried to access incompatible context ‘file:///F|/Untitled-1.swf’

    Any idea?
    Please email me back as I’ve been trying to figure out how the twitter works with Flash and until now I can’t do it
    I’m new to SWX


  3. Hi Mo,

    I’ve just added a note to clarify the issue you are seeing. Here it is again:

    Note: The Flash IDE will give you a security sandbox warning but the application will run correctly (look in the SWX Data Analyzer or trace out the loaded data to check this.) This warning occurs because you are running the SWF in the Flash IDE. If you put the SWF file on the same domain as the SWX gateway, it will also work without requiring any further code. However, if you want to use the Public SWX gateway and deploy your Flash applications to your own server, you must either manually call System.security.allowDomain, or (the recommended way is to) use the SWX Full API.



  4. Rob

    Hi, Aral.

    When I try to get at loader.result, I always get “undefined.” This is even when the result appears in SWX Data Analyzer perfectly, and even when the Flash IDE’s variable dump in the debug menu shows it.

    I’ve tried tracing loader.result, loader.result[n], loader.result[n].whatever … everything is “undefined”

    Can you help explain what this flash novice is missing here?



  5. Rob

    Figured it out; must be a typical beginner mistake. The script has to wait a few moments to get the data back, so you have to delay or repeat (e.g. onEnterFrame) the trace or output.


  6. Hi Rob,

    Happy to hear that you got it sorted.

    I would strongly suggest that you use the SWX ActionScript Library (called the Full API,above) for any sort of real work with SWX RPC. It handles all that for you and gives you a clean interface to work with.

    The native method is great for understanding how SWX works conceptually and for building your own libraries.


  7. Hey

    When I use SWX, I get these errors in SWX itself:

    Location: SWX.as
    Description: 1046: Type was not found or was not a compile-time constant: MovieClip.

    Location: SWX.as
    Description: 1046: Type was not found or was not a compile-time constant: Void.

    Are there bugs in SWX? shouldn’t be Void be void?

    btw: Saw you at Multi-Mania 08, great talk!


  8. I built a widget using your SWX gateway months ago and it’s been working flawlessly. I just discovered that the method friendshipCreate is returning an
    error = “This method requires a POST.” This also happens in your SWX service explorer. Any ideas on why this is happening ? thanks



  9. Ian

    I’m getting this error every time I attempt to use the getNumFriendsUpdates method.

    Fatal error: Cannot use object of type stdClass as array in /home/swxforma/public_html/php/services/Twitter.php on line 543



  10. ecreatures

    I can’t understand the loadMovie expression in the sample code proposed. To me loadMovie is a deprecated expression,I tried the code in Flash and there’s no way I can get it working…


  11. Hi ecreatures,

    loadMovie is used in AS2 and AS1. We have an AS3 version that’s currently in beta. Check out the documentation here: http://swxformat.org/201


  12. Hi widgetDev,

    I am having the same problem with ‘POST’ using the friendshipCreate method and I have my code running on a webserver, so it’s not a GET/POST Flash IDE thing.
    You will notice that it does not work on the SWX public gateway either - so I doubt it will work anywhere else.

    I have created lots of apps and sites that use ‘POST’ before and they have all worked, so I am a bit confused.

    ( like you I have tried many of the other methods and they work fine )

    Cheers

    gingerman



  13. Phil

    Hi Aral, I am currently on the pursuit to create something similar to twittervision. I am trying to do it for a university project but my knowledge on anything more than just plain actionscript and flash is minimal. Are there any tutorials you have made or any links you know of that could point me in the right direction?

    Any help would be very much appreciated,

    Phil



  14. Dan

    When using the second example where you try to grab the specific Member feed from twitter, nothing is returned and in SWX analyzer it gives and error saying Value = Could not authenticate you.

    Why would this be? I could have sworn that is was functioning a week or two ago.


  15. Здравствуйте! Ищу толковый автосерфинг(платят за просмотр сайтов). Подскажите если кому что известно.
    Я нашел толковый автосерфинг vaselisk.net.ru но етого маловато.
    Он хорош более для рекламы чем для заработка.
    Заранее благодарен.



  16. satish

    hi sir,
    I m new novice to SWX. How can i create my own twitter api based on swf files that provide all the functionalty similar to twitter. please help me and suggest me if you have any tutorial url regarding this.


  17. When using the second example where you try to grab the specific Member feed from twitter, nothing is returned and in SWX analyzer it gives and error saying Value = “Could not authenticate you.”

    i am receiving the same error msg, is this due to the fact that clients can make no more than 100 calls per hour perhaps?



  18. fabian

    Hi Aral,

    Nice work on this connection between twitter and flash! I’m still experimenting a little with the search method… In the analyzer everything goes well, i’m getting my results right. But when it comes to the part where flash needs to display these results, the whole thing fails on me. I get “undefined” on everything I’ve tried. Rob said something about a delay, maybe that you or rob can show me how to solve this issue? Thank you very much!

    Kind regards Fabian


  19. @fabian hey,

    if you move the trace a little further down the timeline as a hack, or just add a function to the onEnterFrame it will fix your problem, i had the same problem, seems like it takes a second to initialise the connection to the online API



  20. fabian

    @newstuff thanks, but I don’t think I’m doing this right. If you have some sample to get me on the road I will be very very happy. If I just could have some information coming back after my query…



  21. kb

    a example for send a tweet would be fantastic!!! king regards, kim



  22. md

    What is the best/easiest way to add a social bookmark link for Twitter on my Flash AS3 site. I am brushing up on my Action Scripting, PHP and Ruby on rails. Is the best approach with PHP? Do you have any sample code to share?

    Best Regards,
    M


Leave a Reply

Anti-Spam Protection by WP-SpamFree