(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Facebook Graph API and Silverlight Part 2 – Publishing data

(4 votes)
Lazar Nikolov
>
Lazar Nikolov
Joined Dec 15, 2010
Articles:   7
Comments:   59
More Articles
2 comments   /   posted on Nov 17, 2011
Tags:   lazar-nikolov , facebook
Categories:   Data Access

This article is compatible with the latest version of Silverlight.

This is the follow up of my article Facebook Graph API and Silverlight. Don’t miss to check it out to learn more about the Facebook Graph API and how to access Facebook data in the easy way. 

No API can be complete if you can’t publish data through it. Facebook Graph API sets you no boundaries. This time I focus on posting data in your or your friends’ Facebook profiles – like or comment on a photo, post to someone’s wall, responding to event request as attending, and so on.

The big picture

Again, when publishing to Facebook, you use an access token. Depending on what you need to do, the access token must grant you user data permissions, friends’ data permissions or extended permissions. In most cases you will need combined permissions. To publish stream on public profile (that includes like or comment on a public photo) you need only publish_stream permission from the extended permissions. But, if you want to use your or your friend profile, you must get access token with all sufficient permissions. That means that:

  • When posting a like or comment on a photo in your own profile, you need additional user_photos permission.
  • When posting a like or comment on a friend’s photo, you need to grand friends_photos permission.
  • When responding to an event request, you need the rsvp_event permission from the extended permissions set.

And counting... It’s impossible to cover all scenarios, but you got the logic. Or just experiment with the permission sets. Facebook provides you with the Graph API Explorer to easy generate your access token - Facebook Graph API Explorer.

I think that’s enough for the sufficient permissions. This is only the first step. What you must do after that is issuing a HTTP POST request to the appropriate url. Here are several examples again:

  • If you need to like a photo, you issue a POST request to https://graph.facebook.com/OBJECT_ID/likes, where OBJECT_ID is the id of the photo. You are not limited only to photos. You like any object that has a /likes connection. When liking, you don’t need to send any additional information.
  • If you need to comment a photo, you issue a POST request to https://graph.facebook.com/OBJECT_ID/comments, where OBJECT_ID is the id of the photo. Again, you can comment on any object that has /comments connection in the graph. The difference from liking a photo, here, is that you must send the comment message with the POST request.

These are only examples that you apply in any other scenario. Responding to event request is posting to /EVENT_ID/response, where response is attending, maybe or declined depending on whether you intend to attend the event or not. Posting to wall is sending the request to /PROFILE_ID/feed, where PROFILE_ID is the id of the profile of your friend.

All HTTP requests can be issued in the graph API explorer.

Posting in Silverlight

In my first article in this series, I used WebClient to get Facebook data through the Graph API. This was like a GET request. This time we need a POST request.

After you identify the action for your request (like, comment, etc.), generate the sufficient permission set and make it clear what is the appropriate url you are to post to, it’s Silverlight show time!

Let me first go through liking a photo.

private void FacebookPOSTRequest()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://graph.facebook.com/" + picId + "/likes?access_token=" + AccessToken));
    request.Method = "POST";
    request.ContentLength = 0;
    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}

This code issues a POST request that likes the photo with id picId. The sufficient permission set is in AccessToken. Remember, you can like any object that has /likes connection.

If you need to comment on a photo, you replace /likes in the url with /comments. And, later you must also set the arguments with the comment message. When comment, you don’t need to set ContentLength:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://graph.facebook.com/" + picId + "/comments?access_token=" + AccessToken));
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);

You write the comment message in the RequestReady method:

private void RequestReady(IAsyncResult asyncResult)
{
    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
    Stream stream = request.EndGetRequestStream(asyncResult);
 
    this.Dispatcher.BeginInvoke(delegate()
    {
        // Write the POST arguments
        StreamWriter writer = new StreamWriter(stream);
        writer.Write("message=my comment");
        writer.Flush();
        writer.Close();
        request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
    });
}

You find the required arguments with their names on http://developers.facebook.com/docs/reference/api/.

When liking a photo you just flush and close the StreamWriter after creating it:

StreamWriter writer = new StreamWriter(stream);
writer.Flush();
writer.Close();
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);

The same is valid for the event response. Here is the url for the event response with attending:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://graph.facebook.com/" + eventId + "/attending?access_token=" + AccessToken));

To parse the result from the request, you use the ResponseReady method:

private void ResponseReady(IAsyncResult asyncResult)
{
    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
             
    this.Dispatcher.BeginInvoke(delegate()
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string result = reader.ReadToEnd();
    });
}

Conclusion

In this article the focus was on issuing a POST request in Silverlight to harness the Facebook Graph API publishing capabilities. You learned what you need to do to gain the sufficient permission set depending on the action you do. For accessing Facebook data, read the first article from the series - Facebook Graph API and Silverlight. In part 3 of the series I will show you how to create albums and upload images.

Feel free to leave you comment on everything that isn’t clear! I will be glad to share my answer, if I can.


Subscribe

Comments

  • Geethanjali

    Re: Facebook Graph API and Silverlight Part 2 – Publishing data


    posted by Geethanjali on Mar 06, 2012 13:08

    Hi Lazar Nikolov,

    This article is very helpful. I am trying to update my facebook status from silverlight application. I used this code I am getting

    "Operation is not valid due to the current state of the object."  exception at this line

    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

     Unable to proceed. Please help me........

    Thanks in advance

  • -_-

    Re: Facebook Graph API and Silverlight Part 2 – Publishing data


    posted by on Jan 03, 2013 03:29
    Model ThL W5 Band 2G: GSM 850/900/1800/1900MHz 3G: WCDMA 850/900/1900/2100MHz.

Add Comment

Login to comment:
  *      *       

From this series