Initializing the Facebook Session
Set the Facebook App ID and Windows Store ID values in active session:
using winsdkfb ;
namespace SampleCode
{
public static partial class SampleFunc
{
static void Initialization ()
{
FBSession sess = FBSession . ActiveSession ;
sess . FBAppId = "<Facebook App ID>" ;
sess . WinAppId = "Windows or Windows Phone Store ID depending on target device" ;
}
}
}
#include "pch.h"
using namespace Windows :: Foundation :: Collections ;
using namespace Platform ;
using namespace concurrency ;
using namespace winsdkfb ;
using namespace winsdkfb :: Graph ;
namespace SampleCode
{
void Initialization ()
{
FBSession ^ sess = FBSession :: ActiveSession ;
sess -> FBAppId = "<Facebook App ID>" ;
sess -> WinAppId = "<Windows or Windows Phone Store ID depending on the target device" ;
}
}
Note: During development, you can always use the PhoneProductID from the manifest to get the WinAppId instead of having one Windows Store ID and one Windows Phone Store ID.For a published app you would need the package SID.
Login
Use the following code snippet to login to Facebook.
The sess.LoginAsync()
or sess->LoginAsync()
call launches the Facebook login dialog box for the user to enter his/her username and password.
using System ;
using System.Collections.Generic ;
using winsdkfb ;
namespace SampleCode
{
public static partial class SampleFunc
{
static async void Login ()
{
// Get active session
FBSession sess = FBSession . ActiveSession ;
// Add permissions required by the app
List < String > permissionList = new List < String >();
permissionList . Add ( "public_profile" );
permissionList . Add ( "user_friends" );
permissionList . Add ( "user_likes" );
permissionList . Add ( "user_groups" );
permissionList . Add ( "user_location" );
permissionList . Add ( "user_photos" );
permissionList . Add ( "publish_actions" );
FBPermissions permissions = new FBPermissions ( permissionList );
// Login to Facebook
FBResult result = await sess . LoginAsync ( permissions );
if ( result . Succeeded )
{
//Login successful
}
else
{
//Login failed
}
}
}
}
#include "pch.h"
using namespace Windows :: Foundation :: Collections ;
using namespace Platform ;
using namespace Platform :: Collections ;
using namespace concurrency ;
using namespace winsdkfb ;
using namespace winsdkfb :: Graph ;
namespace SampleCode
{
void Login ()
{
// Get active session
FBSession ^ sess = FBSession :: ActiveSession ;
// Add permissions required by the app
Vector < String ^>^ permissionList = ref new Vector < String ^> ();
permissionList -> Append ( L"public_profile" );
permissionList -> Append ( L"user_friends" );
permissionList -> Append ( L"user_likes" );
permissionList -> Append ( L"user_groups" );
permissionList -> Append ( L"user_location" );
permissionList -> Append ( L"user_photos" );
permissionList -> Append ( L"publish_actions" );
FBPermissions ^ permissions = ref new FBPermissions ( permissionList -> GetView ());
// Login to Facebook
create_task ( sess -> LoginAsync ( permissions )). then ([ = ]( FBResult ^ result )
{
if ( result -> Succeeded )
{
// Login succeeded
}
else
{
// Login failed
}
});
}
}
Logout
This is simply just calling the LogoutAsync()
method.
using System ;
using winsdkfb ;
namespace SampleCode
{
public static partial class SampleFunc
{
static async void Logout ()
{
FBSession sess = FBSession . ActiveSession ;
await sess . LogoutAsync ();
}
}
}
#include "pch.h"
using namespace Windows :: Foundation :: Collections ;
using namespace Platform ;
using namespace concurrency ;
using namespace winsdkfb ;
using namespace winsdkfb :: Graph ;
namespace SampleCode
{
void Logout ()
{
FBSession ^ sess = FBSession :: ActiveSession ;
sess -> LogoutAsync ();
}
}
Profile Picture Control
The SDK provides a ProfilePictureControl that can be populated with the logged in user’s profile picture.
XAML:
<Page
x:Class= "SampleCode.ProfilePictureControlSample"
xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local= "using:SampleCode"
xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:winsdkfb= "using:winsdkfb"
mc:Ignorable= "d" >
<Grid Background= "{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<winsdkfb:ProfilePictureControl x:Name= "ProfilePic" Width= "120" Height= "120" />
</Grid>
</Page>
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Runtime.InteropServices.WindowsRuntime ;
using Windows.Foundation ;
using Windows.Foundation.Collections ;
using Windows.UI.Xaml ;
using Windows.UI.Xaml.Controls ;
using Windows.UI.Xaml.Controls.Primitives ;
using Windows.UI.Xaml.Data ;
using Windows.UI.Xaml.Input ;
using Windows.UI.Xaml.Media ;
using Windows.UI.Xaml.Navigation ;
using winsdkfb ;
using winsdkfb.Graph ;
namespace SampleCode
{
public sealed partial class ProfilePictureControlSample : Page
{
public ProfilePictureControlSample ()
{
this . InitializeComponent ();
// Get active session
FBSession sess = FBSession . ActiveSession ;
if ( sess . LoggedIn )
{
// Get current user
FBUser user = sess . User ;
// Set profile pic
ProfilePic . UserId = user . Id ;
}
}
}
}
#include "pch.h"
#include "ProfilePictureControlSample.xaml.h"
using namespace SampleCode ;
using namespace Windows :: Foundation :: Collections ;
using namespace Platform ;
using namespace concurrency ;
using namespace winsdkfb ;
using namespace winsdkfb :: Graph ;
ProfilePictureControlSample :: ProfilePictureControlSample ()
{
InitializeComponent ();
// Get active session
FBSession ^ sess = FBSession :: ActiveSession ;
if ( sess -> LoggedIn )
{
// Get current user
FBUser ^ user = sess -> User ;
// Set profile pic
ProfilePic -> UserId = user -> Id ;
}
}
Some basic information about the logged in user can directly be accessed through FBSession.ActiveSession.User
.
using winsdkfb ;
using winsdkfb.Graph ;
namespace SampleCode
{
public static partial class SampleFunc
{
static void UserInformation ()
{
// Get active session
FBSession sess = FBSession . ActiveSession ;
if ( sess . LoggedIn )
{
// Get current user
FBUser user = sess . User ;
string userId = user . Id ;
string username = user . Name ;
string locale = user . Locale ;
}
}
}
}
#include "pch.h"
using namespace Windows :: Foundation :: Collections ;
using namespace Platform ;
using namespace concurrency ;
using namespace winsdkfb ;
using namespace winsdkfb :: Graph ;
namespace SampleCode
{
void UserInformation ()
{
// Get active session
FBSession ^ sess = FBSession :: ActiveSession ;
if ( sess -> LoggedIn )
{
FBUser ^ user = sess -> User ;
if ( user )
{
String ^ userId = L"Id : " + user -> Id ;
String ^ username = L"Name : " + user -> Name ;
String ^ locale = L"Locale : " + user -> Locale ;
}
}
}
}