Authentication
Initializing the Facebook Session
Set the Facebook App ID and Windows Store ID values in active session:
#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.
#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.
#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>
#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;
}
}
User Information
Some basic information about the logged in user can directly be accessed through FBSession.ActiveSession.User
.
#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;
}
}
}
}