Click here to Skip to main content
15,888,037 members
Articles / Programming Languages / C#
Tip/Trick

How to Use Facebook C# SDK on Windows

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
19 Jun 2015CPOL2 min read 48.8K   2.2K   14   9
This tip is a simple Facebook app in Windows 8.1 using free tools like Microsoft Visual Studio 2013 Community Edition

Introduction

This tip is a simple Facebook app in Windows 8.1 using free tools like Microsoft Visual Studio 2013 Community Edition. It will guide you from installing a Facebook app through programming the C# SDK for Facebook. It's a Windows Store style app.

Background

I checked some Facebook application examples but none satisfied what I wanted and that is the new Facebook asynchronous C# SDK. Knowledge of C# and XAML is required, in particular, the await async keywords.

Using the Code

Download the source file and build it using MSVS 2013 community edition which is free. There is no binary file since it must be installed in the store or on your computer through MSVS.

Installing a Facebook Desktop App

  1. Go to Tools->nuget package manager -> nuget console
    to install Facebook.dll and FacebookClient.dll type this: Install-Package Facebook.Client
  2. Right click references to add a reference to facebook.dll and facebook.client.dll
  3. Go to developers.facebook.com-> myapps -> add a new app (like in figure 1)
  4. Select a name and click advanced
  5. Click settings
  6. Click advanced then native or desktop app (like figure 2)
  7. (very important) Select Embedded browser and OAuth Login (like in figure 3)
  8. Click yes, then save at the bottom
  9. Add an XML file named facebookconfig.xml to the main folder of your project, it looks like this:
    XML
    <?xml version="1.0" encoding="utf-8" ?>
        <Extensions>
            <Facebook AppId="your app id here" />
        <Extensions>
    <xml>
    

    That's it. This is the only place we will need the AppID, the AppSecret will not be needed.

Image 1

Fig 1

Image 2

Fig 2

Image 3

Fig 3

Updating the MainPage.cs and MainPage.xaml

Add to MainPage.cs:

C#
using Facebook;
using Facebook.Client;
using System.Threading.Tasks; // Task<>
using System.Diagnostics; // Debug

Here is the method to get user information:

C#
private async static Task<dynamic> GetUser(String token)
{
    var client = new FacebookClient();
    dynamic user = await client.GetTaskAsync("/me",
        new { fields = "first_name,last_name,gender,locale,id,name,email,picture,location",
        access_token = token });

       return user;
 }

Add to your XAML file xmlns:fbc ="using:Facebook.Client.Controls”. Between the grid, add this:

XML
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
            <fbc:LoginButton Name="fbclb" />
            <TextBox Name="tbUser" />
            <Button Name="btSend" Content="get user" Click="OnButtonUser" />
            <Button Name="btPublish" Content="Publish" Click="OnButtonPublish" />
            <fbc:FriendPicker Name="fbcfp" />
            <fbc:ProfilePicture />
            <fbc:PlacePicker />
       </StackPanel>
</Grid>

We added a login button, a friend picker, a profile picture and a place picker control. The friend picker doesn't really work anymore, only users who are your friends and are connected to the same app as you, will be added. But we add it anyway so we can easily get the AccessToken. The login button does all the hard work to authenticate into Facebook.

The functions GetUser and PublishStream were adapted from this article on MSDN magazine.

Additional Information

There is a very nice tool in Facebook to explore the so called "graph API" ( a series of get or post http elements). You can try it here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionPackage.appxmanifest file missing Pin
Member 127799956-Oct-16 23:51
Member 127799956-Oct-16 23:51 
AnswerRe: Package.appxmanifest file missing Pin
fioresoft8-Oct-16 18:15
fioresoft8-Oct-16 18:15 
QuestionNot a windows 8.1 app Pin
Adminpass201027-Oct-15 0:39
Adminpass201027-Oct-15 0:39 
QuestionTrouble installing Facebook.Client from NuGet Pin
Bounze11-Jul-15 10:59
Bounze11-Jul-15 10:59 
AnswerRe: Trouble installing Facebook.Client from NuGet Pin
fioresoft28-Jul-15 11:17
fioresoft28-Jul-15 11:17 
QuestionProject file is missing *NT Pin
Flem100DK22-Jun-15 10:43
Flem100DK22-Jun-15 10:43 
AnswerRe: Project file is missing *NT Pin
fioresoft22-Jun-15 20:12
fioresoft22-Jun-15 20:12 
GeneralRe: Project file is missing *NT Pin
Flem100DK22-Jun-15 20:50
Flem100DK22-Jun-15 20:50 
GeneralRe: Project file is missing *NT Pin
fioresoft22-Jun-15 21:19
fioresoft22-Jun-15 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.