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
- set FARM (ex.: http://farm”+flickrXML..photo[i].@farm+”.static.flickr.com/)
- set SERVER (ex.: +flickrXML..photo[i].@server+”/”)
- set Photo ID with underscore (ex.: +flickrXML..photo[i].@id+”_”+”)
- set Secret code (ex.: flickrXML..photo[i].@secret+)
- 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.
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.
You can do this with as3flickrlib http://code.google.com/p/as3flickrlib/
First of all you must login and then you can use API to work with comments and so on
Hi luca.
I tried to figure out before to do this using the as3flickrlib, and I just double check again the documentation of the code http://as3flickrlib.googlecode.com/svn/trunk/docs/index.html but I still can’t find the way to implement the photo comments using the library. Sorry for taking your time, but do you know which class and method in the as3flickrlib do the magic?
Greetings.
I think is setMeta method.
Take a look here: http://as3flickrlib.googlecode.com/svn/trunk/docs/com/adobe/webapis/flickr/methodgroups/Photos.html#setMeta()
Hi Luca.
Thanks again! I saw that too, but the problem is that is the meta information for the photo itself and it doesn’t create a comment from an user in a photo, I think that probably this web service is not implemented in the library.
This is the web service I want to use in the Flickr API
http://www.flickr.com/services/api/flickr.photos.comments.addComment.html
Ok mate, so you can work with SWX: http://swxformat.org/php/explorer/ you can find that service!
it’s so easy add SWX to your project.
Have a good job 😉
Sure.
I’ll do that.
Thanks a lot for the advice.
you are welcome 😀
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.
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