DCSIMG
July 2008 - Posts - Gady Elkarif's Blog

Gady Elkarif's Blog

July 2008 - Posts

Develop Facebook Application using Facebook Developer Toolkit

Develop Facebook Application using Facebook Developer Toolkit

Lets see how it easy to create new application in Facebook, using Facebook Developer Toolkit.

Create new Facebook Application 

Add the Developer Application to your account at Facebook.

Now you can set up a new application:

 

Set up  new Facebook Application

Facebook will provide you API Key and Secret Key you will use later.

Set up the following properties: 

  • FBML/iframe - iframe
  • Application Type - Website
  • Callback URL - <Your callback URL, see next step> for example: http://localhost:3243/Facebook/Default.aspx

Create ASP.NET Web Site

Create in Visual Studio new ASP.NET Web Site.

After downloading binaries of Facebook Developer Toolkit, add to your project references to:

  • Facebook.dll
  • Facebook.WebControls.dll 

Go to your application in Facebook and setup the Callback URL to the URL of the Default.aspx page.

It is ok to put for now localhost.

Go to the code of Default.aspx.cs and insert the following sample code:

public partial class _Default : System.Web.UI.Page

{

    Facebook.Components.FacebookService m_fbService = new Facebook.Components.FacebookService();

 

    private const String FACEBOOK_API_KEY = "<YOUR API KEY>";

    private const String FACEBOOK_API_SECRET = "<YOUR API SECRET>";

 

    protected void Page_Load(object sender, EventArgs e)

    {

        m_fbService.ApplicationKey = FACEBOOK_API_KEY;

        m_fbService.Secret = FACEBOOK_API_SECRET;

        m_fbService.IsDesktopApplication = false;

 

        String sessionKey = Session["facebook_session_key"] as String;

        String userId = Session["facebook_userId"] as String;

 

        String authToken = Request.QueryString["auth_token"];

 

        if (!String.IsNullOrEmpty(sessionKey))

        {

            m_fbService.SessionKey = sessionKey;

            m_fbService.UserId = userId;

        }

        else if (!String.IsNullOrEmpty(authToken))

        {

            m_fbService.CreateSession(authToken);

 

            Session["facebook_session_key"] = m_fbService.SessionKey;

            Session["facebook_session_key"] = m_fbService.UserId;

            Session["facebook_session_expires"] = m_fbService.SessionExpires;

        }

        else

        {

            Response.Redirect(@"http://www.Facebook.com/login.php?api_key=" + m_fbService.ApplicationKey + @"&v=1.0");

        }

        if (!IsPostBack)

        {

            Collection<Facebook.Entity.User> friends = m_fbService.GetFriends();

        }

    }

}

 

Now, you can test your application. This is a very simple application, and I'm going to add to this application some features in future posts.

Enjoy!