Flickr mesh-up with actionscript 3, REST or SWX

Today I spend my afternoon to configure a part of a full Flash website that interact with Flickr and that retrieve data from my account without any appication authentication but only with my Flickr key.

First of all I was really impressed about how many languages you can use to bring Flickr photos and use in own desktop application or website (Real Basic, cUrl, Perl, Delphi, C…).

When you want retrieve photos from Flickr you must have a Flickr key that is totally free and you can activate one directly in the right section of Flickr site.
In the same section you can find whole documentation and lots of services expose to retrieve any kind of data from a photo, from Exif data to user or friends informations.
For AS3 we have a couple of interesting library; first one is as3flickrlib very well documented but it’s so long to start work with it because you must to find a frob ID then a token and finally you can start to work with Flickr API.
The other one is SWX, that is written by Aral Balkan and it’s too easy to mesh up any kind of popular service like Flickr, Twitter and so on.
Aral gives us availability to use SWX Service Explorer to take a look at the data and how to mesh up your application with public API, I really love it on Flash Lite too!

This is very interesting because you can retrieve all API in this library and in few line of code you can work with public services.
This is a simple example to call Flickr services with SWX, you can download source code too:

swx = new SWX();
swx.gateway = GATEWAY_PHP;
swx.encoding = "POST";
var callDetails:Object =
{
serviceClass: "Flickr",
method: "photosSearch",
args: ["", "", "#mart3"],
encoding: "POST",
timeout: 5,
resultHandler: resultPhotos,
faultHandler: faultHandler
};
swx.call(callDetails);

Another interesting way to work with Flickr API without any authentication is using REST API.
REST (Representational state transfer) is a very simple way to request data from a database, update, delete them or to create new record using HTTPHeaders and XML.
You must know that AS3 doesn’t have PUT and DELETE method but there are some libraries that create a workaround to solve this limitation and they work very well with REST (as3httpclientlib for example)
I start to approach REST a couple of months ago with Ruby on Rails and I think is amazing.

Flickr gives us REST API too, and you can see that is so simple create a Flickr gallery with REST and Actionscript 3.

We have some API that works without authentication like photos search and we use this service to retrieve data for our Flickr gallery.
First of all we take a look at service documentation on Flickr, you can see that we have a lots of opportunity to find data on Flickr; we can search photos via userID, tags, text on title or description and so on.

In Actionscript 3 we’ll use URLLoader class and prepare some url to retrieve XML data from REST.

private const FLICKR_SERVICE = "flickr.photos.search";

private const API_FLICKR:String = "c6c31464878505403f9bbbf6214ae413";

private const PATH_SEARCH_FLICKR:String = "http://api.flickr.com/services/rest/";

		

public function FlickrRest(){

			

	var varsFlickr:URLVariables = new URLVariables();

	varsFlickr.method = FLICKR_SERVICE;

	varsFlickr.api_key = API_FLICKR;

	varsFlickr.tags = "#mart3";

			

	var req:URLRequest = new URLRequest();

	req.url = PATH_SEARCH_FLICKR;

	req.method = URLRequestMethod.POST;

	req.data = varsFlickr;

			

	var urlL : URLLoader = new URLLoader();

	urlL.addEventListener(Event.COMPLETE, retrieveImages);

        urlL.load(req);

			

}

You can download source code and final sample from this link.
Finally I’d like to explain how to compose photo’s url to retrieve them on Flickr.
Flickr url is composed by some variables that you can find in REST XML or SWX result object

  1.  set FARM (ex.: http://farm”+flickrXML..photo[i].@farm+”.static.flickr.com/)
  2. set SERVER (ex.: +flickrXML..photo[i].@server+”/”)
  3. set Photo ID with underscore  (ex.: +flickrXML..photo[i].@id+”_”+”)
  4. set Secret code (ex.: flickrXML..photo[i].@secret+)
  5. last parameters is which size you want to get photos, you can use “_s.jpg” for small size, “_m.jpg” for medium, “_l.jpg” for large and so on.

I hope that could be interesting for many developers, if you have any suggestions or idea about mesh up with Flickr, feel free to drop a comment.

Advertisements

11 thoughts on “Flickr mesh-up with actionscript 3, REST or SWX

  1. Hi.

    I am very interested in what you are doing, I am at the same point, just that I am more interesting in not only getting the img from an specific user in my flex app but I need to comment them too. Do you know how can I do this?

    Greetings.

  2. Hi Luca.

    Have you use the authentication mechanism that comes with the SWX with the Flickr service on AS3 (FLEX).

    I am trying to make i work, but I a having problems when retrieving the token.

    Greetings.

  3. Hi
    Thanks for sharing this great code.
    Could you show me how to add the photo into a movie clip from the library.
    I’m getting error – 5006: an actionscript file can not have more than one externally visible definition:
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s