Making authenticated calls to flickr

There are few things to know , when getting photos from your stream , adding comments and overall doing adding and deletion of your photos. As with flickr you can take a look at this url => http://www.flickr.com/services/api/auth.spec.html  for authentication spec to learn more. But with Athena (formerly known as LINQ.Flickr) it's pretty easy to get things going. Last week, I made an update to the FlickrXplorer (The MVC starter project you don't want to miss :-)) project that now enables you to add comments for photos. There is a one click login that will take you to flickr, ask you once for the permission to grant the app for data access and finally will take you back to the place from where you were left off.

image

Now, concept with MVC app is to call a controller url , and finally point flickr to a second controller url that will bring the user to last page, if everything works fine. This whole thing is done by Athena on behalf without a single line of code on client app.

To start off , lets create a context class.

FlickrContext context = new FlickrContext();

Authentication done in two ways, first one is the auto authentication.

var query = from photo in context.Photos
                    where photo.SearchText == "Home" && photo.ViewMode == ViewMode.Owner
                    select photo;

This query tries to search in my photo stream with word "Home",  where the owner is the caller himself. This will automatically take me to the flickr authentication page. However, this is good for desktop based application, where it will fire up the browser , let me authenticate and on closing, it will continue from next line of code. This will also work for web apps, but in case if we need to do something else on successful return, we need a manual way for sure.

context.Authenticate();

Lets say, I call /Photo/Security/Auth url which runs the above line in the controller class that will eventually fire the authentication  process. Here to note that once the authentication process is complete all the successive calls will return Authentication token class, which we can use for showing user status or like "Welcome XYZ...".

AuthToken token = context.Authenticate();

if (token != null)
{
   People people = (from p in context.Peoples
                               where p.NsId == token.UserId
                               select p).Single();
// do the rest.
}

Finally, if we are on a public computer, we surely don't want to leave without logging off. So, there should be a manual call for that as well.

context.ClearToken();

And, it says all. Technically, Athena stores a token in the App Directory for desktop application with the given path in config for FlickrSettings. In web application, it stores the token in user's browser cookie with 30 days of lifeline for current site which of course goes out as soon as you logout. Token is used by flickr for validating authenticated calls and also by the API to make useful decisions in minimizing service calls.

Hope that gives some light on those who likes to give authenticated features to their personal photo app without much of hair loss.

Again, check things out : www.codeplex.com/linqflickr.

kick it on DotNetKicks.com

No Comments