Tuesday, March 13, 2007

FlickrNet API and Microsoft Ajax

I had to create a simple application that I could put temporarily on the front page of my website. Application is a modern, written in the light of all the latest developments in the industry Ajax.

It seemed to me an interesting exercise annex, which after a certain period of time images on a page one picture of the set.

So, the task will split into three :

  1. Find, grouping, host set of pictures.
  2. Implement able to access the photos and receive link.
  3. Implement a mechanism to update the page one photo.

And the bottom line for all this must be make as simple as possible, without the use of third-party developers.

Preparation photos

It is tempting to implement this paragraph was version host photos side by side on the site to the directory and then simply to collect the photo, but it is ridiculous for serious developers and primitive, so we did five years ago, and then it was the rule ...

It is our view that we should not duplicate essentially nowhere ... Now, remember, if I have photos, of course there is! My almost abandoned account at flicr. Add to the existing fotkam still quite new and has received decent base of about 100 photos, which are themselves based on the freely on the remote and, more importantly free hosting.

FlickrNet API

Flickr was chosen also because there is an API for access to the site and functionality.

And even there API Kits. Of which there are FlickrNet API, which supports people from Microsoft. ( News Feeds ).

So in order to get started, we need to get the API Key. This link, you can create your own API Key to the Shared Secret. Also check your User ID.

Now we have to implement it.

Create an empty website. In Web.Config write your settings.

<configSections>
    <section name="flickrNet" type="FlickrNet.FlickrConfigurationManager,FlickrNet"/>
</configSections>
<flickrNet apiKey="YOUR API KEY" secret="YOUR SHARED SECRET" cacheDisabled="true">
</flickrNet>
<appSettings>
    <add key="UserId" value="YOUR USER ID"/>
</appSettings>

Disable the cache, not to encounter problems with the rights to publish the draft.

Creating Default.aspx where CodeBehind creating an object to work with Flickr

Flickr flickr = new Flickr();

Login

private string fTempFrob;
...
fTempFrob = flickr.AuthGetFrob();
string authUrl = flickr.AuthCalcWebUrl(AuthLevel.Read);

Get all public picture of our User Id

Photos publicPhotos = flickr.PeopleGetPublicPhotos(ConfigurationManager.AppSettings.Get("UserId"));

Store them in an array of class

private Photo[] photos;
...
photos = publicPhotos.PhotoCollection.ToPhotoArray();

All these actions are performed only once at the first Page_Load

if (!IsCallback) {
    ...
}

Now we can go at any time in our fotopodborke.

Microsoft Ajax

If you still do not have the downloading and setting. Then put form UpdatePanel, inside which lay Image. Rolling on the form also Timer, for example, ask the interval 20,000 milliseconds. At Timer1_Tick write code follow

Image1.ImageUrl = photos[rnd.Next(photos.Length)].MediumUrl;

And the Page_Load add line

private Random rnd;
...
rnd = new Random(DateTime.Now.Millisecond);

Certainly here missed some stuff in more detail you can see in MyFlickrProject.zip

No comments: