Recently I had to create a proof of concept for a Facebook application. The idea was to create an external application where one can send messages to his Facebook friends without being logged in FaceBook.
Finally, I opted to use the Facebook.NET framework over the FaceBookToolkit. I ran into issues when using FaceBookToolkit and I found FaceBook.NET more reliable and light.
- Download and unzip the FaceBook.Net framework from here
- Create a new Visual Studio website project and reference the FaceBook.Net dll.
- Now its time to create an application in Facebook. This is a good link to create a FaceBook application. Just follow the instructions. But select the radiobutton “Use Iframe” in “Canvas Page Url” since we are creating an external website not a FBML application.
- Secondly, to share your application with other users (one requires a minimum of 5 users before the application can be published) select “Yes” in the radio button that says “Can your application be added on Facebook?” in “Editing your settings”. A new section called “Installation Options” is displayed. Fill the mandatory fields. Also, unchecked the checkbox “Developer Mode”.
- Also note that for debugging purpose one can put a local url and thus can debug! Later the urls can be updated when the application is deployed.
- Now back to VS to start development. Although, the application will be external one still need to create a page residing in an iframe in Facebook so other users can “install” the application. Also, since we want to perform actions on behalf of users that are not logged in we need an additional step to get a permanent session key. Here are the steps (thus screens) to be created:
- From our external site ask new users to “install” the application in FaceBook. This page should redirect to the following URL: http://www.facebook.com/login.php?api_key=APIKEY where APIKEY is the APIKEY generated when the application was created in STEP 4 above.
- Once the user logs in FaceBook and accept our invitation he is redirected to the Callback URL (again set in STEP 4) above. This time the user comes with a temporary session key (maximum 24 hours).
- With this session we have to create a permanent session to store so we use this session to perform REST API calls on behalf of the user. We ask the user to generated a permanent key by putting a link on our page. The link should open the following page: http://www.facebook.com/code_gen.php?v=1.0&api_key=API_KEY
- This page creates a token which the user has to copy and paste in our page. Thus ideally the above URL should open in a new tab;)
- The code is the following to create a permanent session using the token provided:
FacebookRequest fr = new FacebookRequest(fbApplication.Session);
String userID = "";
bool expires = false;
String secret = "";
String sessionKey = fr.CreateSession(txtToken.Text, out userID, out expires, out secret);
Now with this session key we can perform any operation we want. Of course, as
requested by FaceBook a link must be provided to remove these rights.
But REST calls can be done as in the following example:
To send notifications:
fbApplication.Service.Notifications.SendNotification(users,
txtMessage.Text, null);
fbApplication is the control provided by FaceBook.Net
EnableExternalBrowsing="true"
ApplicationName="nudgebuddies" OnSessionCreating="fbApplication_SessionCreating" />
event. In this event we set our permanent session. The code for this event should
be something as follows:
protected void fbApplication_SessionCreating(object sender, FacebookSessionCreatingEventArgs e)
{
if (Request.Params["id"] != null)
{
String userID = Request.Params["id"];
String sessionKey = Request.Params["sessionid"];
e.SetInfiniteSession(sessionKey, userID);
}
}
Hope its useful to someone!
No comments:
Post a Comment