问题
I created a universal app via Microsoft's AppStudio. I tried adding Facebook authentication to the app by following the 'Scrumptious tutorial' (http://facebooksdk.net/docs/phone/tutorial/).
When I run the app on my phone, I can never get to the facebook login page, because the following line: await App.FacebookSessionClient.LoginAsync("user_about_me,read_stream");
always results in the following exception:
System.NotImplementedException: Not implemented
at Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri, Uri callbackUri)
at Facebook.Client.FacebookSessionClient.d__24.MoveNext()
The source of the exception is this call in the FacebookSessionClient.cs (facebook-client package):
var result = await WebAuthenticationBroker.AuthenticateAsync(options, startUri, endUri);
It seems that this function is not implemented for the phone. I am still wondering how it is possible that the turial, which refers to the exact same code would work.
回答1:
It's not implemented for 8.1 yet. If you want to use Facebook authentication in 8.1 you can use the following approach:
In your App class:
private const string RedirectUrl = "https://www.facebook.com/connect/login_success.html";
private static readonly IReadOnlyCollection<string> Permissions = new[] { "email", "offline_access" };
protected override void OnActivated(IActivatedEventArgs args)
{
base.OnActivated(args);
var continuationActivatedEventArgs = args as IContinuationActivatedEventArgs;
if (continuationActivatedEventArgs == null)
return;
var webAuthenticationResult = ((WebAuthenticationBrokerContinuationEventArgs)continuationActivatedEventArgs).WebAuthenticationResult;
if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
var facebookClient = new FacebookClient();
var result = facebookClient.ParseOAuthCallbackUrl(new Uri(webAuthenticationResult.ResponseData));
if (!result.IsSuccess)
{
// Process unsuccessful authentication
}
else
{
// Process successful authentication
var accessToken = result.AccessToken;
}
}
}
// Authentication method, this method should be invoked when you click Facebook authentication button
public void AuthenticateAndContinue()
{
var loginUrl = GetLoginUrl();
WebAuthenticationBroker.AuthenticateAndContinue(loginUrl, new Uri(RedirectUrl));
}
private Uri GetLoginUrl()
{
var parameters = new Dictionary<string, object>();
parameters["client_id"] = "YourFacebookApplicationId";
parameters["redirect_uri"] = RedirectUrl;
parameters["response_type"] = "token";
parameters["display"] = "touch";
parameters["mobile"] = true;
parameters["scope"] = String.Join(",", Permissions);
var facebookClient = new FacebookClient();
return facebookClient.GetLoginUrl(parameters);
}
I put everything in one place just for example purposes, it's better to separate fb authentication logic. You can find this approach here MSDN Windows Phone 8.1 Web Authentication samples
来源:https://stackoverflow.com/questions/26307586/does-the-facebook-net-client-sdk-support-universal-apps-apps-generated-via-ap