Home SQL Express
Home Explore by Interest Explore by Product Samples and Resources Download Support
Simple
Download Now

Getting Started with the Facebook Developer Toolkit


Windows Forms Quickstart

  1. Start a new Windows application project
    • File–New–Project
    • Windows application
      Visual C#
      Create Application
      Visual Basic
      Create Application
  2. Add the FacebookService component from the toolbox to the Form's component tray.
    Facebook ComponentsFacebook service
  3. Provide the API Key and Secret values.
    Add API key
  4. Drag a FriendList from the toolbox onto the design surface for the form.
    Add Friendlist
  5. Hook up the Form Load event:
    • Find Load in the Event list.
    • In the property window, type Form_Load. Press Enter.
      Connect Form Load
  6. In the generated Form_Load method, set the Friends property of the FriendList control to the Collection returned by calling the GetFriends method of the FacebookService. As shown here:

    Visual C#:

            privatevoid Form_Load(object sender, EventArgs e)

            {

                friendList1.Friends = facebookService1.GetFriends();

            }


    Visual Basic:

        PrivateOverloadsSub OnLoad()

            friendList1.Friends = facebookService1.GetFriends()

        EndSub

  7. Press F5 to run the application.

Web Quickstart

  1. In Visual Web Developer 2005 Express edition, start new web application project.
    • File–New–Web site

      Visual C#:
      Create Web site
      Visual Basic:
      Create Web site
  2. Switch to Design View of Default.aspx.
    Design View
  3. On Default.aspx, drag a friendlist from the toolbox.
    Add Friendlist
  4. Add the following code to the Default.aspx code file.
    • Configure the API key and secret.

      Visual C#

      publicpartialclass _Default : Page {

          Facebook.Components.FacebookService _fbService =

              new Facebook.Components.FacebookService();

       

          protectedvoid Page_Load(object sender, EventArgs e) {

       

              // ApplicationKey and Secret are acquired when you sign up for

              _fbService.ApplicationKey = "YOURKEYHERE";

              _fbService.Secret = "YOURSECRETHERE";


      Visual Basic:

      PartialPublicClass _Default

          Inherits Page

          Private _fbService As Facebook.Components.FacebookService = _

              New Facebook.Components.FacebookService()

       

          ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As EventArgs)

       

              ' ApplicationKey and Secret are acquired when you sign up for

              _fbService.ApplicationKey = "YOURKEYHERE"

              _fbService.Secret = "YOURSECRETHERE"

    • Set the IsDesktopApplication property of the FacebookService component.

      Visual C#:

      _fbService.IsDesktopApplication = false;


      Visual Basic:

              _fbService.IsDesktopApplication = False

  5. Check if we have already stored the Facebook session information or if the auth_token is in the query params. We will store what we know about the current user’s Facebook Session in a server-side variable. We will then check that variable to see if we already have established a Facebook session on behalf of the current user.

    Visual C#:

            string sessionKey = Session["facebook_session_key"] asString;

            string userId = Session["facebook_userId"] asString;

     

            // When the user uses the facebook login page,

            // the redirect back here will will have the auth_token in the query params

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


    Visual Basic:

            Dim sessionKey AsString = TryCast(Session("facebook_session_key"), String)

            Dim userId AsString = TryCast(Session("facebook_userId"), String)

     

            ' When the user uses the facebook login page, the redirect

            ' back here will will have the auth_token in the query params

            Dim authToken AsString = Request.QueryString("auth_token")

  6. If we have an established session, set it into our instance of the service.

    Visual C#:

            if (!String.IsNullOrEmpty(sessionKey)) {

                _fbService.SessionKey = sessionKey;

                _fbService.UserId = userId;

            }


    Visual Basic:

            ' We have already established a session on behalf of this user

            If (NotString.IsNullOrEmpty(sessionKey)) Then

                _fbService.SessionKey = sessionKey

                _fbService.UserId = userId

  7. If not, check if we have the auth_token in the query params. If we do, it means we just got called from the Facebook login page.

    Visual C#:

            elseif (!String.IsNullOrEmpty(authToken)) {

                _fbService.CreateSession(authToken);

                Session["facebook_session_key"] = _fbService.SessionKey;

                Session["facebook_userId"] = _fbService.UserId;

                Session["facebook_session_expires"] = _fbService.SessionExpires;

            }


    Visual Basic:

                ' This will be executed when facebook login redirects to our page

            ElseIf (NotString.IsNullOrEmpty(authToken)) Then

                _fbService.CreateSession(authToken)

                Session("facebook_session_key") = _fbService.SessionKey

                Session("facebook_userId") = _fbService.UserId

                Session("facebook_session_expires") = _fbService.SessionExpires

  8. If neither, we need to redirect the user to the Facebook hosted login page.

    Visual C#:

            else {

                Response.Redirect(@"http://www.facebook.com/login.php?api_key=" +

                    _fbService.ApplicationKey + @"&v=1.0");

            }


    Visual Basic:

            Else

                Response.Redirect("http://www.facebook.com/login.php?api_key=" & _

                    _fbService.ApplicationKey & "&v=1.0")

            EndIf

  9. Set the Friends property of the friendlist.

    Visual C#:

            if (!IsPostBack) {

                // Use the FacebookService Component to populate Friends

                MyFriendList.Friends = _fbService.GetFriends();

            }


    Visual Basic:

            If (Not IsPostBack) Then

                ' Use the FacebookService Component to populate Friends

                MyFriendList.Friends = _fbService.GetFriends()

            EndIf

  10. Press F5 to run the application.

Page view counter