|
|
|
|
Getting Started with the Facebook Developer Toolkit
Windows Forms Quickstart- Start a new Windows application project
- File–New–Project
- Windows application
Visual C#
.jpg) Visual Basic
.jpg)
- Add the FacebookService component from the toolbox to the Form's component tray.
.png) .png) - Provide the API Key and Secret values.
.png) - Drag a FriendList from the toolbox onto the design surface for the form.
.png) - Hook up the Form Load event:
- Find Load in the Event list.
- In the property window, type Form_Load. Press Enter.
.png)
- 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 - Press F5 to run the application.
Web Quickstart- In Visual Web Developer 2005 Express edition, start new web application project.
- File–New–Web site
Visual C#:
.png) Visual Basic:
.png)
- Switch to Design View of Default.aspx.
.png) - On Default.aspx, drag a friendlist from the toolbox.
.png) - 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
- 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") - 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 - 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 - 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 - 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 - Press F5 to run the application.
| |
|
|
|
|
|
|
|