2014-10-10 31 views
2

我通过微软的AppStudio创建了一个通用的应用程序。 我试着按照'精美教程'(http://facebooksdk.net/docs/phone/tutorial/)向应用程序添加Facebook身份验证。Facebook .NET客户端SDK是否支持通过AppStudio生成的通用应用程序/应用程序?

当我跑我的手机上的应用程序,我可以永远到不了Facebook登录页面,因为下面一行:await App.FacebookSessionClient.LoginAsync("user_about_me,read_stream");

总是会导致以下异常:

System.NotImplementedException: Not implemented
at Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri, Uri callbackUri) at Facebook.Client.FacebookSessionClient.d__24.MoveNext()

的例外的来源是FacebookSessionClient.csfacebook-client包): var result = await WebAuthenticationBroker.AuthenticateAsync(options, startUri, endUri);

看来这个功能没有为手机实现。我仍然想知道指代完全相同的代码的turial是如何工作的。

回答

7

它尚未在8.1中实现。如果你想在8.1使用Facebook的身份验证,您可以用下面的办法:

在你的App类:

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

我把一切都放在一个地方,只是作为一个例子,这是更好地分离FB验证逻辑。 你可以在这里找到这个方法MSDN Windows Phone 8.1 Web Authentication samples

+0

你知道为什么SDK没有在8.1上实现时调用这个函数吗?这是Windows Phone API的最新变化吗? – Gabor 2014-10-16 21:28:11

+1

Windows Phone sdk标记为'WebAuthenticationBroker.AuthenticateAsync',因为已弃用而评论:“AuthenticateAsync不适用于以Windows Phone 8.1开头的版本,而是使用AuthenticateAndContinue或AuthenticateSilentlyAsync”,但Facebook API在FacebookSessionClient.LoginAsync方法中使用此方法,所以我们有NotImplementedException从WebAuthenticationBroker抛出。 – 2014-10-17 07:16:50

相关问题