Flickr API for Windows Phone 7 – Part 5 – Activity – User Comments

DZone 's Guide to

Flickr API for Windows Phone 7 – Part 5 – Activity – User Comments

· Mobile Zone ·
Free Resource

When a user signs up for Flickr, chances are he/she will not only post his/her own pictures, but also view other published photo materials. And sometimes (depends on the user) comments will be posted on other people's photos. In this article, I am showing how to obtain the list of comments a user posted on other people's content.

This is made possible with the help of flickr.activity.userComments. Knowing that you follow the "Flickr API for Windows Phone 7" series, you should already have an Activity class inside your project (in the Core folder). You only need to add one method to it:

public void GetUserComments(string apiKey, string signature, string authToken, HelperDelegate helperDelegate, string page = "", string resultsPerPage = "")
    helperDelegateInstance = helperDelegate;

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

    if (page != "")
        URL += "&page=" + page;
    if (resultsPerPage != "")
        URL += "&per_page=" + resultsPerPage;

    client.DownloadStringAsync(new Uri(URL));

GetUserComments is very similar to GetUserPhotos - the only major difference is the fact that it doesn't have the timeframe parameter - the API endpoint won't accept it and it is not documented.

Obviously, the URL is different - but once again, the only change here is the method name and the absence of references to timeframe. One thing worth noticing here is the fact that I am using the same event handler for data download completion - client_DownloadStringCompleted. As I mentioned in my prvious article, there is only one common property that stores the result for both methods, called LastActivityResult. This property can be set by both methods when the JSON data is received, therefore I am taking this opportunity to make the event handler common - shared between two methods and triggered depending on the instance call.

Let's test this method now. Somewhere in the main page (it's good if you have the test page I mentioned in my previous article) use this snippet:

param = new Dictionary<string, string>();
param.Add("api_key", "APIKEY");
param.Add("auth_token", "TOKEN");
param.Add("format", "json");

Core.Authentication.GetSignature(param, "SECRET", () =>
    txtLog.Text += "\nGetting list of comments...";
    Core.Activity activity = new Core.Activity();

    activity.GetUserComments(apiKey, Core.Authentication.Signature, "TOKEN", () =>
        txtLog.Text += "\n" + activity.LastActivityResult;

One thing to mention here - the use of the API key, the token and the secret will be so frequent, that you might want to store them as settings in the isolated storage later on. Since right now I am only showing how to build the core, the experimental code is fine, but eventually for easier reuse it would make sense to make these values persistent.

Speaking of the token, once you get the full token, you can reuse it multiple times instead of generating a new one everytime the application starts - your app is already authorized for a specific user and all you have to do is reference the existing full token whenever requested.

Let's see what we get when the code above is executed!

Congratulations! Now you can keep track of your activity on other people's content! This becomes quite handy when you don't remember where was your last comment or what you said about a specific picture. And although this method is quite useful, it is recommended that you do not call it more than once an hour - for Flickr performance reasons.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}