2014-10-10 89 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

相關問題