lundi 14 décembre 2015

Using the new .NET Facebook SDK in Windows 10 apps (UAP) C# and setting up you Facebook developer account

I was migrating one of my applications that was using the old Facebook SDK and that did not work at all in my new UAP application.  Search the web i found that Facebook had developed a new SDK for UAP apps.  Here is a small tutorial of what i had to do to make this SDK work with my current code.

What you need to have:
- FacebookId
- cloned FacebookSDK: https://github.com/Microsoft/winsdkfb
- Blank UAP app

I am using Visual Studio 2015 and a UAP application so I had to add this project to my application:
FBWinSDK\FBSDK-UWP\FBSDK-UWP\FBSDK-UWP.vcxproj
and then add the reference to my blank app.

your application should look as follows:



 Next we are going to need to get the Id of our application :

string SID = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();

you will need to add this using of this code to work:

using Windows.Security.Authentication.Web;

Next we need to get the Facebook session, we will need our Facebook Id and our Windows Application id as follow:

FBSession sess = FBSession.ActiveSession;
sess.FBAppId = "<Facebook App ID>";
sess.WinAppId = "<Windows or Windows Phone Store ID depending on the target device>";

you will need to add this using of this code to work:

using Facebook;

And lastly we need to get the login prompt and set the permitions, the permitions are to be set in a list of strings:

// Add permissions required by the app 
        private static readonly List<string> PermissionList = new List<String>() { "user_about_me", "email", "publish_actions" };

And here is the login code:

 FBPermissions permissions = new FBPermissions(PermissionList);

        
                // Login to Facebook
                FBResult result = await sess.LoginAsync(permissions);

                if (result.Succeeded)
                {
                    // Login successful
                    return sess.AccessTokenData.AccessToken;
                }
                else
                {
                    // Login failed
                    return null;

                }

In the application that you can find on Github, you will fnd evrything on one page:


    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        // Add permissions required by the app 
        private static readonly List<string> PermissionList = new List<String>() { "user_about_me", "email", "publish_actions" };



        public async Task<string> LogIntoFacebook()
        {
            //getting application Id
            string SID = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();

            // Get active session
            FBSession sess = FBSession.ActiveSession;
            sess.FBAppId = Dailymotion.Core.Constants.FacebookAppId;
            sess.WinAppId = SID;

            //setting Permissions
            FBPermissions permissions = new FBPermissions(PermissionList);

            try
            {
                // Login to Facebook
                FBResult result = await sess.LoginAsync(permissions);

                if (result.Succeeded)
                {
                    // Login successful
                    return sess.AccessTokenData.AccessToken;
                }
                else
                {
                    // Login failed
                    return null;

                }
            }
            catch (InvalidOperationException ex)
            {
                //error handling
                return null;
            }
            catch (Exception ex)
            {
                //error handling
                return null;
            }
        }

        private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            //get the access token to login
            var result = await LogIntoFacebook();
        }
    }

Setting up your account


Next you will need to access you developer account on Facebook: https://developers.facebook.com/apps/

You will need to create a new app, you fill find you app Id here and then you will need to setup the Windows Store SID that you can find in your application and add it  here under the setting panel.



Update 1:
To make this work you also need to go to Facebook Developer website: https://developers.facebook.com
under: your app name -> Settings -> Advance:
And then under: Valid OAuth redirect URIs you need to add: https://www.facebook.com/connect/login_success.html
Save and you are good to go now! enter image description here


Github code: here

SDK; https://github.com/Microsoft/winsdkfb

More information here:
http://blogs.windows.com/buildingapps/2015/07/14/windows-sdk-for-facebook/

1 commentaire:

  1. If you are looking into generating cash from your visitors via popup ads, you should run with one of the most reputable networks: PopCash.

    RépondreSupprimer