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:
- Open the SWX Data Analyzer
- In Flash, create a new FLA.
- Create a new movie clip and give it the instance name loader.
- 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)
The The new SWX Twitter API (and how to build your first Flash Twitter mashup in four lines of code!) article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.
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
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
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.
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?
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.
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.
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!