Who knew back in 2004 that Facebook would have such a big influence in everybody’s lives ten years on? I used to abhor Facebook and saw no interest in it whatsoever; now, I set my Facebook profile up nicely so that it contains only feeds and people I am really interested in. Today, I will show you how to create a very basic app using Windows 8.1 and VB.
As with any other social media platform, you cannot just jump straight into the code. Because Facebook makes use of a different platform, it is difficult to know where to get started. Luckily, Facebook provides an SDK (Software Development Kit) for you to use and do with what you like. You can find the Facebook SDK at this link.
There are also several open-source Facebook libraries that you can use. Some of the Facebook libraries available are the following:
To get started with whichever option you choose to develop with, you still need a developer’s key. You obtain this key by navigating to this link and registering your application. Once all the steps have been completed, you are ready to start. Even then, there is still a lot to do…
A Sample Project
Today’s sample project’s purpose is created solely to display a list of my friends.
Start Visual Studio 2013 and start a new VB Windows Store project, as shown in Figure 1:
Figure 1: The project
Add a reference to Facebook.dll, as shown in Figure 2:
Figure 2: Add a reference
Before I continue, the above file forms part of this SDK, which you should download before continuing.
Now that the basic set up is done, you can design your page to resemble mine, as depicted in Figure 3.
Figure 3: Our design
You can name the objects anything you like, but keep in mind that I may have used different names for the objects.
You have to understand one thing: You always need authorization before your app can be run on the Facebook platform, or even make use of the Facebook library calling. Okay, you ask: how do I get permission? The answer is simple: OAuth.
Now that you know about OAuth, you may proceed with this article.
Add the following objects to your application. Add these in the General Declarations section:
Private fb = New FacebookClient() Public AppId As String = "YourAppIDSuppliedByFacebook" Public AppSecret As String = "YourAppSecretSuppliedByFacebook" Private Const ExtendedPermissionsNeeded As String = _ "email,offline_access"
Here, I created a new FacebookClient object that gives us access to all of Facebook’s functionalities. The next two variables are very important. Upon registering your app in Facebook, Facebook supplied you with an application ID, as well as a secret code. This secret code is to further ensure that the app is indeed valid. The last object is to give your application certain permissions. In this case, these permissions ininclude email as well as Off line access.
Private Sub MainPage_Loaded(sender As Object, _ e As RoutedEventArgs) Handles Me.Loaded Dim oauthResult As FacebookOAuthResult Dim AccessToken Dim Authorized Dim MyName As String Dim MyEmail As String If fb.TryParseOAuthCallbackUrl(e.OriginalSource, _ oauthResult) Then If oauthResult.IsSuccess Then AccessToken = oauthResult.AccessToken Authorized = True Else AccessToken = "" Authorized = False End If If Authorized Then fb = New FacebookClient(AccessToken) Dim result = fb.[Get]("me") MyName = result.name MyEmail = result.email End If End If End Sub
Here, I created an OAuth object. I then made use of the fb object’s TryParseOAuthCallbackUrl to get access to Facebook’s libraries, to get access to my profile’s information.
The last step in our project is to get a list of friends. Add the next piece of code:
Private Sub btnGet_Click(sender As Object, _ e As RoutedEventArgs) Handles btnGet.Click Dim MyFriends As New Dictionary(Of String, String)() Dim myInfo = fb.[Get]("\me\friends") For Each [friend] In myInfo.data MyFriends.Add([friend].id, [friend].name) lstFriends.Items.Add([friend].id & " " & _ [friend].name) Next End Sub
I created a dictionary object to hold the details of all my friends. I then made reference to the Get method of my fb object to get my list of friends. Lastly, I loop through each object inside MyInfo and add them sequentially to my listbox.
This was a short article; sorry about that. My aim is to point you in the right direction. I like to teach people how to fish; that is how I learned, hence this article. Nevertheless, I hope you enjoyed it as much as I enjoyed showing it to you. And, don’t overlook that you can download code for this article at the bottom of the page. Until next time, cheers!