In this example I am creating a .NET 2.0 Windows Forms Application (I used Visual Studio 2008) in C# to upload a photo to your Flickr account and then create a new set (album) and assign that photo to this set.
Here are the steps to do this:
- Get the API Key from Flickr (This key and its secret code are required to access the service)
- Go to Your API Keys section on Flickr.
- Click on ‘Apply for a new key online’ link.
- Complete the process. I used the Non-Commercial option to get the key immediately. You will have to provide the details of your application. Once this is done, click on Edit key details and update all the information.
- Note down the Key (it will look like 3e1f3c83c5e92ead353be33162fr4883) and Shared Secret (it will look like 700f4d5220833d34)
- Get the FlickrNet API Library (This is a .NET wrapper class library around Flickr API and makes it very easy to call the services)
- Go to http://flickrnet.codeplex.com/
- Download either just the binaries or the source code if you would like to debug into the library.
- FlickrNet.dll is the only one file you will require.
- Design the application interface
- I created a WinForm application as shown below with options to authenticate the user and to choose a picture file for uploading.
You need to do an authentication to the flickr account you are trying to access. This is a one time process and is done by directing the user to a URL (generated using the service) which will then open up in a browser. This process is initiated using ‘Start Auth’ button. Once the user logs into the flickr account and approve the access request from this application, he or she will have to come back to this application and click on ‘Complete Auth’. In this method, we get a ‘Token’ which uniquely identifies the user. This token can be saved and used again to access this user’s account without requiring further authentication process.
- Add code to call Flickr API methods
First thing is to add a reference to the FlickrNet (either direct dll or the project reference if you use source code) in your project. Next declare the required variables.
Then in the Form’s load event, provide your API Key and Share Secret codes and create the Flickr object. Event handlers are also added here.
Now let us open an image for uploading…
and then start authentication.
This uses something called Frob (cookie kinda stuff) and generates a URL which is then opened up in the user’s default browser. Remember, at this point, user will have to sign in and authenticate the access request from this application.
Once the user completes the authentication and comes back to the application, we gets the token for that user.
The token is assigned to the Flickr object to enable access to that user’s account. It can be saved for later use without doing this authentication process again.
Now we are all set to upload the picture to flickr.
Here we set the progress bar values and then use a new thread different from UI thread to call the Upload method. We also create a new photo set and add this photo to it.
The last thing is to make sure that the progress bar is updated properly.
That’s it… Hope this was useful to you and as always, feel free to ask any questions you may have on this…