Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Downloading User Images from Flickr in C#

0.00/5 (No votes)
29 Apr 2015 1  
Getting current logged user Images who logged from ASP.NET application

Introduction

In a requirement, I have to get the logged user images from third party sites such as Picasa and Instagram & Flickr. While Googling, I was successful with Picasa, but unfortunately, I was not successful with Flickr. After hours and hours of R&D, I succeeded with Flickr. Now, I would like to share this Flickr code with other developers.

Using the Code

Here, while dealing with Flickr, first of all, we need to Register our App with Flickr and then we need to get the Secret Key & Token from Flickr.

By following this URL, we can register our App:

and the next thing is using that Flickr Secret and Token, we need to get the user details and number of images that user may contain in Flickr. Remember that Flickr will only provide a max of 1000 images of user to any app, here I am using C# with MVC. I wrote all the code in Controller which is to get user Images.

I wrote my API secret and Token in Web.Config file, and I am accessing them from my Controller.

  public class FlickrController : Controller
    {
        public  FlickrController()
        {
         
        }
       private User oUser;
       Flickr flicker = new Flickr(ConfigurationManager.AppSettings["flickrApi"].ToString(),
           ConfigurationManager.AppSettings["flickrSharedSecret"].ToString());
    }

And next is we need to ask Flickr to authorize the users who are logging from our App so that an authentication and authorization code and Redirection code was necessary after authorizing the user.

 public ActionResult FlickrCall()
        {
            FlickrNet.Cache.CacheDisabled = true;
            string flickrUrl = flicker.AuthCalcWebUrl(AuthLevel.Read);
            OAuthRequestToken token = FlickrManager.GetRequestToken(flickrUrl);
            var url1 = flicker.OAuthCalculateAuthorizationUrl(token.Token, AuthLevel.Read);
            Session["oauth_token"] = token.Token;
            Session["oauth_tokensecret"] = token.TokenSecret;
            return Redirect(flickrUrl);            
        }

This method will take the user to Flickr Site and authorize the user and then it will redirect to our App.

As we are in MVC, here we need to use an attribute for the method which was asking the user Images, ie[HttpGet].

Remember that immediately after redirecting, Flickr Application will send us the dynamic secret code, the first thing we need to do is immediately in the Get method, we need to save/store that in a clientside statemanagement, i.e., in query string/cookie, or... etc.

Here is the Get method which makes everything about the user (calculates number of images & gets user name & user Id & user name). Here, one more thing we need to remember is that we need to identify the logged user dynamically and our code needs to get his details. Or else, if we have only a single user, directly we can pass his email/username. But we have multiple users across dynamically, that's why our code needs to calculate the user details. Here is our Get method.

        [HttpGet]
        public ActionResult FlickrCallBack()
        {
            //these are my Flickr models (you can write your models)
            FlickrViewModel mod = new FlickrViewModel
            {
                ImageUrlList = new List<string>(),
                UserLibraryDetails = new List<UserLibrary>()
            };
            //End of my Flickr Models
            string frob = Request.QueryString["frob"];
            ---------->This is the Dynamic secret as we diss
            try                                                  //-cussed above
            {
                oUser = (User)Session["oLoggedInUser"];

                if (oUser != null)
                {

                    OAuthAccessToken oat = new OAuthAccessToken();
                    oat.Token = Session["oauth_token"].ToString();
                    oat.TokenSecret = Session["oauth_tokensecret"].ToString();
                    Auth auth = flicker.AuthGetToken(frob);
                    string uid = auth.User.UserId;
                    PhotoCollection photos = flicker.PeopleGetPublicPhotos(uid, 
                    0, 50, SafetyLevel.None,PhotoSearchExtras.PathAlias);
                                                                
                    if(photos!=null)
                    {
                      //Loop the no of Photos
                      foreach(Photo ph in photos)
                      {
                       //Do Your Stuff Here ....:)
                      }
                    }
}
} 

}//close the Get method

}//close the controller

All the above things we need to write after importing predefined namespace using FlickrNet;

PhotoCollection photos = flicker.PeopleGetPublicPhotos(uid, 
0, 50, SafetyLevel.None, PhotoSearchExtras.PathAlias);

As we had seen this statement in the above method:

  • uid-->logged userid of Flickr or else if we can replace this with user mailId(wxyz@yahoo.com), but we have dynamic logged users, so it is always recommended to identify the logged user dynamically
  • 0-->starting Image
  • 50-->50th Image of user (here in need only 50 Images of Logged User) if we need more than we can replace this with any other number but not more than 1000 (max given by Flickr)

Photo-->from the above foreach loop, we can notice Photo (Photo is the predefined class in .NET Librarynamespace using FlickrNet).

Points of Interest

In fact, I had done R&D about this requirement from many sites and I had read many articles, but unfortunately I was unsuccessful of getting the user images... the only thing is I was not storing the dynamic secret immediately from the Get method... else everything was fine before, but I am unable to get UserID/UserEmail.

Once I caught that Dynamic Secret, it will be maintained for getting the User Details (UserId/UserEmail).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here