Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...