Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Login for Windows Phone 8.1

I am trying to figure out a simple code for logging in facebook from a Windows Phone 8.1 app (C#).

As the Facebook Client from NuGet doesn't target Windows Phone 8.1, it seems I'd have to write some extra code. As I read in this facebook post, I'd need to launch an Uri to invoke the Login Dialog. That much, I managed to do :

 await Launcher.LaunchUriAsync(FacebookUri.DisplayLoginDialog);

where DisplayLoginDialog is a static string object with the required necessary data on the request (appId, productId, permissions, etc). I was redirected to the facebook app, to accept that my app requires such permissions.

And I accepted.

So what now? How do I get a response or something with the access_token? I have researched a lot for this but I wasn't able to find relevant posts.

The same facebook link from above, says at the section of Handling the Login Dialog that :

If someone successfully logs in, the URI association of your app will be automatically triggered, meaning they will be sent to your app, along with an access token:

msft-{ProductID}://authorize/?
access_token={user-access-token}&
expires_in={expiration-time-of-token}

But I am confused on how to actually use this in C#.

How do I get a response with the access token after the login suceeded or and error code and error message if it failed, like it is written in the facebook post?

like image 592
VasileF Avatar asked Aug 06 '14 19:08

VasileF


Video Answer


1 Answers

In the Package.appxmanifest file, go to 'Declarations' Tab and add a Protocol Declaration.

In the Name Field, enter your Microsoft product id as 'msft-PRODUCT_ID' [product id without dashes].

In the App.xaml.cs file, add the following code

    protected override void OnActivated(IActivatedEventArgs args)
    {
        if (args.Kind == ActivationKind.Protocol)
        {
            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;

            Uri responseUri = eventArgs.Uri;

           //Now you can use responseUri to retrieve the access token and expiration time of token

        }

        base.OnActivated(args);
    }
like image 81
MohanRajNK Avatar answered Oct 06 '22 00:10

MohanRajNK