Accessing Flickr Using .NET

Social media sites are 10 a penny these days. There are sites for messaging, sites for videos, sites for photos, and a whole slew more.

One of the oldest ones, and one that's still going strong, is Flickr. Now owned by Yahoo, if you have a Yahoo email account, you should automatically also be able to set up a Flickr account Flickr is used by millions of people all over the world to store and make available entire photo collections for you to view and search through.

Like many social media sites, Flickr offers access to its services via APIs. These APIs allow you to do many things, from uploading and organizing your collections, right through to finding out about users of the service and searching photos for various keywords.

Getting Access

The first thing you need before you can start to make use of the service is an account and an API key. To get an account, you have to sign up for a regular Yahoo account; the simplest way to do this is to browse to https://eu.edit.yahoo.com/registration, and hen fill in the requested information.

Once you have a Yahoo account, you then can head to https://www.flickr.com/services/api/misc.api_keys.html and follow the instructions to create a new API key.

I can't take you through either of those step by step because I've already set my accounts up, but both of them are very straightforward and easy to understand.

One thing I will say, though. If you're creating this just for playing around with, make sure your API key setup is a private one If you create a public API key, Flickr will expect you to make your app public and available via the "App Garden" for others to use.

Once you have an active key, browsing to https://www.flickr.com/services/apps/by/<your user ID here> (where <your user ID here> should be replaced with your Flickr user ID [in my case, 18885387@N00]) should present you with something that looks similar to what's shown in Figure 1:.

Flickr1
Figure 1: Flickr API keys list

Once you have your API key, it's then time to fire up Visual Studio.

For this post, I'm just going to take you through quickly searching for keywords in a command line app. However, no matter where you're using this, the steps are the same.

Start Visual Studio and create a simple command line program. Head to NuGet, and then search for and install "FlickrNet" by "Sam Judson."

Flickr2
Figure 2: FlickrNet NuGet package

Please note, also, that unlike many packages, FlickrNet also has specific packages for specific platforms, so if you're using Windows Phone 7, Silverlight, or the Compact framework, please make sure you select the appropriate package.

Flickr3
Figure 3: Alternate FlickrNet packages

Once you have your API key, using the library to access Flickr is very simple. First, you need to create a Flickr access object using your ApiKey:

const string apiKey = "YourApiKeyGoesHere";

Flickr flickr = new Flickr(apiKey);

Once you have a Flickr access object, you can use the search options and photo collections classes as follows:

var options = new PhotoSearchOptions
   { Tags = searchTerm, PerPage = 20, Page = 1 };
PhotoCollection photos = flickr.PhotosSearch(options);

foreach (Photo photo in photos)
{
   Console.WriteLine("Photo {0} has title '{1}' and
      is at {2}", photo.PhotoId, photo.Title,
      photo.LargeUrl);
}

PerPage is as you might expect: how many results you want to return for a given page, and 'Page' is the page number. In reality, I've had 200 per page for one page, and still not hit an upper limit. It may even be possible to return all your search hits on one page.

To round this post off, make sure your "Program.cs" file looks as follows:

using System;
using FlickrNet;

namespace FlickrSearch
{
   class Program
   {
      static void Main(string[] args)
      {
         if(args.Length != 1)
         {
            Console.WriteLine("Usage is FlickrSearch
               <search term>");
            Console.WriteLine();
            Console.WriteLine("EG:");
            Console.WriteLine("FlickrSearch \"Polar Bear\"");

            return;
         }

         const string apiKey = "yourapikeygoeshere";

         Flickr flickr = new Flickr(apiKey);

         string searchTerm = args[0];

         var options = new PhotoSearchOptions { Tags = searchTerm,
            PerPage = 20, Page = 1 };
         PhotoCollection photos =  flickr.PhotosSearch(options);

         var line = 1;
         foreach (Photo photo in photos)
         {
            Console.WriteLine("{3} - Photo {0} has title '{1}'
               and is at {2}", photo.PhotoId, photo.Title,
               photo.LargeUrl, line);
            line++;
         }

      }
   }
}

When compiled, this will give you a small command line program that will allow you to specify a keyword or search phrase and display the ID, title, and URL of the first 20 matches found on Flickr for images matching the term.

.NET getting you down? Want to know the ".NET way" of doing something? Drop me a comment below, or reach out to me on Twitter as @shawty_ds and let me know your .NET ills. I'll see if I can find a way out of them for you.



About the Author

Peter Shaw

As an early adopter of IT back in the late 1970s to early 1980s, I started out with a humble little 1KB Sinclair ZX81 home computer. Within a very short space of time, this small 1KB machine became a 16KB Tandy TRS-80, followed by an Acorn Electron and, eventually, after going through many other different machines, a 4MB, ARM-powered Acorn A5000. After leaving school and getting involved with DOS-based PCs, I went on to train in many different disciplines in the computer networking and communications industries. After returning to university in the mid-1990s and gaining a Bachelor of Science in Computing for Industry, I now run my own consulting business in the northeast of England called Digital Solutions Computer Software, Ltd. I advise clients at both a hardware and software level in many different IT disciplines, covering a wide range of domain-specific knowledge—from mobile communications and networks right through to geographic information systems and banking and finance.

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date