Showing posts with label flickr. Show all posts
Showing posts with label flickr. Show all posts

Sunday, February 20, 2011

Fetching and displaying images from flickr

I was starting on a website which should retrieve images from Flickr. This needed some investigation so I want to share it with you.

First thing you'll have to do is to request an API key. Nowadays you can login with your Google or Facebook account which is really cool of yahoo. Add this key to your appSettings
and add the following code to retrieve a list of urls:
WebRequest tokenRequest = WebRequest.Create(string.Format(
    "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={0}&user_id={1}",
    Uri.EscapeDataString(ConfigurationManager.AppSettings["FlickrKey"]),
    Uri.EscapeDataString("51706638@N07")));
tokenRequest.Method = "GET";

WebResponse tokenResponse = tokenRequest.GetResponse();
var photos = new List<string>();
var xml = new XmlDocument();
xml.Load(new StreamReader(tokenResponse.GetResponseStream()));
foreach (XmlNode node in xml.ChildNodes[1].ChildNodes[0].ChildNodes)
{
    photos.Add(string.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}_m.jpg",
        node.Attributes["farm"].Value,
        node.Attributes["server"].Value,
        node.Attributes["id"].Value,
        node.Attributes["secret"].Value));
}
I used the flickr.photos.search to search all images of a specified user. You can check the misc urls of the Flickr API to see how the url can be build. Off cource the code above can be changed to make use off Xml Serialization, but for now reading the attributes of the photo's is enough for me.