A blog about web technology.

Sunday, February 8, 2009

ASP.NET facebook application “Hello World!”

Here is a quick article to make a quick facebook application in ASP.NET that can be added to facebook pages as well.

First you need to setup a new application in facebook. You can do so in http://www.facebook.com/developers. I am not writing details of the application setup. Here is one nice link for its help: http://developers.facebook.com/get_started.php

Then download ‘Facebook Developer Toolkit’ from http://www.codeplex.com/FacebookToolkit and add references to your web-project.

Add a new aspx page, clean up all the contents of the page, even tag and tag. Facebook application does not support those tags. In this article, I will render contents using Response.Write()

In codebehind, inherit the page class from facebook.web.CanvasFBMLBasePage

Then create a page init handler subroutine as shown below:


Protected Sub Init2(ByVal s As Object, ByVal e As EventArgs) Handles Me.Init

'These code lines MUST be in Init event handler

If String.IsNullOrEmpty(Request.QueryString("fb_page_id")) Then 'The user is viewing application, force them to login to facebook
'and allow the application's access to the user info

MyBase.RequireLogin = True
MyBase.Page_Init(s, e)

'contents of the facebook app
Response.Write("Hello World")

Else
'The user is not viewing the application, but facebook is requesting FBML

Dim fbPageID As Long = CLng(Request.QueryString("fb_page_id"))
Dim FB As New facebook.Components.FacebookService

FB.ApplicationKey = ConfigurationManager.AppSettings("APIKey")
FB.Secret = ConfigurationManager.AppSettings("Secret")
FB.IsDesktopApplication = False

Dim pFBML As String = "Hello World!"

FB.API.profile.setFBML(fbPageID, pFBML, "", "")

'this is the text that facebook users sees
Response.Write("You have successfully set profile FBML")

'setting FBML does not need further processing
Response.End()

End If

End Sub






No comments:

Post a Comment