In my last couple posts, I went over logging a user into Facebook via Facebook Connect, getting that user’s information and then grabbing some of the profile pictures for their friends. All of this was quite simple and brought to you by the Silverlight Facebook SDK. This post will grow more on the first post and will show you how to publish to the user’s Wall and News Feed as well as update their status.
Tell the World
One of the key things that any Facebook application should do is publish messages to the user’s stream on their Wall and News Feed. This helps spread the word that people are actually using your application as well as showing scores and updates for that user and giving some insight into why people should use your app. The first part of this equation revolves around the privacy settings that Facebook has in place. Since we are going to be doing more than just getting information, we will need the user of our application to grant extended permissions to us.
There are a couple of ways to do this, but the easiest one requires us to just add some javascript. We haven’t had to do anything to our javascript files yet, but I assure you this will be quick and painless. In your application, locate the fblogin.js file. This is usually located in the root of your web project, but in my examples has been put in a scripts folder that located at the root of the site. Once you crack that bad boy open, locate the facebook_init method. Inside this method there will be a FB.init method call which is part of the Facebook Javascript Client Library. Update this method as follows:
// original method callFB.init(appid, "/xd_receiver.htm");// updated method callFB.init(appid, "/xd_receiver.htm", { permsToRequestOnConnect: "publish_stream"});
The change we are making is to add in a comma seperated list as a parameter to our FB.init method that tells Facebook what extra permission(s) we want the user to authorize. In this example, we are just passing in publish_stream which will allow us to publish the user’s news stream and wall as well as update their status. A complete list of the extra permission that you can request are here. Also, once the user has authorized your application, they won’t be asked again unless they remove the setting in their profile.
News Feed
So once the user has given us permission, we use the Facebook.Rest.Api.Stream.PublishAsync method call to publish to the user’s stream. When doing this, we have a couple of options. We can go the boring route of just putting up a message, or we can get fancy and add in things like links, images, and videos. The simple call looks something like this:
_facebookApi.Stream.PublishAsync(message, StreamPublishCompleted, null);
where message is just a string that we pass in the for the message the we would like to post. To add in attachments and a littler more detail, some more work is involved:
// create new attachmentattachment a = new attachment();a.name = "This is a Test";a.caption = "This is cool";a.href = "http://www.evanjohnston.com";a.description = "Here is a description that goes with this post. Here is more content to make this longer.";// create new attachment mediaattachment_media_image i = new attachment_media_image();i.href = "http://www.evanjohnston.com";i.src = "http://www.evanjohnston.com/misc/cheerssmaller.jpg";i.type = attachment_media_type.image;// create new list of attachment mediaList media = new List();media.Add(i);// add the media to the attachmenta.media = media;// this could be the id of a user to post the message tostring targetId = string.Empty;// create message stringstring message = "Testing publishing to the stream from the Silverlight Facebook SDK.";// make the api call_facebookApi.Stream.PublishAsync(message, a, new List(), targetId, 0, StreamPublishCompleted, null);
Since there is quite a bit going on here, I’ll just defer to the Facebook Wiki on this topic for a more thorough explanation. The gist of what is happening is that we are creating an attachment for the message and then adding an image to this attachment so that there is more to publish than just text.
Status Update
To show another example of what we can do once the user has give our application extended permissions, we will go ahead and update their current status as well. To do this we go with the Facebook.Rest.Api.Status.SetAsync method call. This call is straightforward and looks like this:
_facebookApi.Status.SetAsync(status, SetStatusCompleted, null);
where the status variable is a string that contains the text we want to use. It’s just that simple.
Conclusion
So in this post we expanded on our last foray into the Facebook SDK and were able to show what is required to actually publish information to the user’s status and news feed. Hope you enjoyed this brief look at the Silverlight Facebook SDK.
Code!
Here is a download of a small demo application that uses the steps that were just discussed. In order to run the application, you will need to set up your own Facebook application. Until next time, Sláinte!