Accessing Images with Flickr's API
Over the past few years, there has been an explosion of Internet functionality unseen since the early days of the dotcom boom. Cool new sites and web-based apps have been springing up every week and, while few will be more profitable those classic dotcom failures, making a pile of cash from venture capital doesn't seem to be the point anymore. Now it seems that the ideal result is to have your funky idea purchased by one of the behemoths warring for Internet domination: Google, Yahoo!, and eBay.
Flickr is one such cool web site purchased by Yahoo!, providing a blend of photo hosting and social networking that's made it a hit among both serious and casual photography buffs. Thanks to the Flickr API, anyone with some knowledge of application development can throw together custom photo-related apps. In this article, we'll take a look at the API itself, discuss authentication, explore a sample of the available features, and put together a demo application.
Flickr provides XML-based interfaces through REST, XML-RPC, and SOAP. Generally these three means of messaging feature differing levels of complexity, but since Flickr's API interactions exclusively involve passing strings back and forth, none of these interfaces is terribly complex. That may seem odd when we're talking about an API built around sharing binary photos, but the API itself really has no means for the transmission of binary data. The API will never actually deliver a photo, instead providing a URL pointing to the photo. And, while there is a programmatic way to upload photos, that process doesn't exactly fit into the API itself, relying instead on a manual HTTP POST.
Because we're really only dealing with UTF-8 encoded strings here, REST provides the simplest interface, so that's what we'll examine. REST requests are sent to a single URL, followed by the method name that you're calling and a list of name=value pairs in alphabetical order. This is Flickr's base example:
In this example, the method name is flickr.test.echo and there is a single key called name with the value value.
Following is a real example, which calls the method flickr.photos.search, retrieving all the photos for a certain user ID:
To see an XML REST response you can simply click the above link. However, as the keys have been modified, you'll receive an error response like this:
<rsp stat="fail"> <err code="100" msg="Invalid API Key (Key not found)"/> </rsp>
Modify the URL above to have valid token, user, and API keys, and you'll receive something like this:
<?xml version="1.0" encoding="utf-8" ?> <rsp stat="ok"> <photos page="1" pages="1" perpage="100" total="7"> <photo id="37452653" owner="53543999%40N10" secret="99fb80fa1e" server="24" title="Photo One" ispublic="1" isfriend="1" isfamily="1" /> <photo id="37452586" owner="53543999%40N10" secret="99aa66a3b7" server="27" title="Photo Two" ispublic="1" isfriend="1" isfamily="1" /> <photo id="37452551" owner="53543999%40N10" secret="993ad599da" server="32" title="Photo Three" ispublic="1" isfriend="1" isfamily="1" /> <photo id="37452457" owner="53543999%40N10" secret="99e2354277" server="33" title="Photo Four" ispublic="1" isfriend="1" isfamily="1" /> <photo id="37452426" owner="53543999%40N10" secret="99c503519b" server="24" title="Photo Five" ispublic="1" isfriend="1" isfamily="1" /> <photo id="37452383" owner="53543999%40N10" secret="9909faefae" server="32" title="Photo Six" ispublic="1" isfriend="1" isfamily="1" /> <photo id="37452359" owner="53543999%40N10" secret="999590fb28" server="29" title="Photo Seven" ispublic="1" isfriend="1" isfamily="1" /> </photos> </rsp>
With a framework like .NET's, which supports easy XML serialization, it's simple enough to write a quick app that talks directly to the REST interface and transforms the XML responses directly to objects. It's not much more complex in other languages that aren't so integrated with XML, even if you must manually parse the XML. However, given the wealth of API wrappers available for most popular languages, taking the time to do is unnecessary.
Flickr API implementations are available that cover everything from ActionScript for Flash apps, Java, and .NET to support for PHP, Perl, Python, and Ruby. If your language of choice isn't featured, of course, you'll have to come up with your own solution. But if it is, one of the existing API implementations will likely meet your needs and save you a lot of time.
For code examples in this article we'll be using the Flickr.NET API kit. This kit provides .NET methods and functions to perform the Flickr API calls as well as a variety of helper functions, such as retrieving a binary stream for photos, given a URL.