Mobile Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Flickr API for Windows Phone 7 – Part 8 – Contacts

10.05.2010
| 6974 views |
  • submit to reddit

There are probably a couple of people you know who also use Flickr. And most likely, you have them added as contacts on your Flickr page. You can retrieve the contact list via Flickr API as well, so in this Windows Phone 7 application there will be the possibility to get  a list of contacts and see the most active of them.

There are three methods here:

•    getList – gets the list of contacts for a specific user.
•    getListRecentlyUploaded – gets the list of contacts for a specific user who made recent photo/video uploads. Also shows the number of uploads.
•    getPublicList – gets the public list of contacts for a specific user.

In this article, I will only cover getList and getListRecentlyUploaded, since the application will be authorized anyway and there is no need to get the public list of contacts – the list will be there anyway via getList. If you plan on creating a Flickr library, you might follow the steps here to implement this call as well (will simply be a modified URL and set of parameters).

I created a Contacts class, that will be instantiated. It has an instance of HelperDelegate that will help when the task will be invoked asynchronously. Standard class template:

using System;
using System.Net;
using System.Windows;
 
namespace FlickrWP7.Core
{
    public class Contacts
    {
        private HelperDelegate helperDelegateInstance;
 
        public string LastContactResult { get; set; }
    }
}

IMPORTANT NOTE: You will be working with sensitive information here. Not secret, but sensitive – chances are that due to some privacy concerns, some people do not want to share in public their real names and nicknames associated with them. Therefore, make sure that you use the methods below only to display data in the context of this application.

So let’s take a look at the implementation for getList.

public void GetList(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, Filter filter = Filter.Disabled,string page = "",string perPage ="")
{
    helperDelegateInstance = helperDelegate;

    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.contacts.getList&api_key={0}&auth_token={1}&api_sig={2}&format=json",
            apiKey,authToken,signature);

    if (filter != Filter.Disabled)
        URL += "&filter=" + filter.ToString().ToLower();

    client.DownloadStringAsync(new Uri(URL));
}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    LastContactResult = e.Result;
    helperDelegateInstance();
}

A very familiar structure, however you can see that there is one parameter that is an instance of Filter. This parameter helps you filter the contacts – you can show either contacts who are your friends, family, both or neither. So in fact, Filter here is a public enum:

public enum Filter
{
    Disabled,
    Friends,
    Family,
    Both,
    Neither
}

By default, when the user does not specify a filter, it is automatically set to Disabled (therefore it is an optional parameter). If it is not Disabled, then the lower case version of the enum item is added to the URL.

Try this method on the main page:

Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("api_key", apiKey);
param.Add("method","flickr.contacts.getList");
param.Add("auth_token", "TOKEN");
param.Add("format", "json");

Core.Authentication.GetSignature(param, "SECRET", () =>
{
    txtLog.Text += "\nTrying to get the contact list...";
    Core.Contacts contacts = new Core.Contacts();

    contacts.GetList(apiKey, "TOKEN",Core.Authentication.Signature, () =>
    {
        Dispatcher.BeginInvoke(new Action(() => { txtLog.Text += "\n" + contacts.LastContactResult; }));
    });
});

Now, to get the list of contacts that performed recent uploads, you can use this method:

public void GetListRecentlyUpdated(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, UpdatedFilter filter = UpdatedFilter.Disabled, string dateUploaded="")
{
    helperDelegateInstance = helperDelegate;

    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.contacts.getListRecentlyUpdated&api_key={0}&auth_token={1}&api_sig={2}&format=json",
            apiKey, authToken, signature);

    if (filter != UpdatedFilter.Disabled)
    {
        if (filter == UpdatedFilter.FriendsAndFamily)
            URL += "&filter=ff";
        else
            URL += "&filter=all";
    }
    if (dateUploaded != "")
    {
        URL += "&date_lastupload=" + dateUploaded;
    }

    client.DownloadStringAsync(new Uri(URL));
}

This method is already tied to another enum – UpdatedFilter:

public enum UpdatedFilter
{
    Disabled,
    FriendsAndFamily,
    All
}

It provides fewer options, so I wasn’t able to use the previous enum – otherwise it might create confusion on correct parameters to be used.

NOTE: The dateUploaded parameter should be specified as a UNIX timestamp – otherwise no correct results will be returned.

Now you are able to keep up to date with your contacts. In case you don’t want to type the code shown above by yourself, you can easily download Contacts.cs here and add it to the Core folder in the existing project.