WP8 – Facebook登录问题

我正在尝试使用Windows Phone 8上的Facebook C#SDK在Facebook上validation用户。为此,我在此处遵循以下代码: FacebookLoginPage.xaml.cs

但我面临的问题是,每当我输入用户名和密码进入打开以validation用户身份的对话框时,我就会看到以下页面:

WP8 FB登录问题

在此之后,我的程序不会重定向到Landing页面,这是一个单独的视图。 我看到的其他解决方案建议隐藏WebView不适用,因为身份validation被抽象为单个LoginAsync函数调用。 关于该怎么做的任何建议?

当它检测到Windows Phone webbrowser控件时,FB似乎对其重定向脚本进行了一些更改。

C#SDK的作用是生成登录页面为“ http://www.facebook.com ….”。 当您在webbrowser控件上打开此URL时,它会被重定向到“ http://m.facebook.com …”,它会显示FB登录页面的移动版本。

这在以前没有问题,但最近,当FB进行重定向时,它还从URL中剥离参数“display = page”。 然后发生的是,当成功进行FB登录时,将打开“login_success.html”页面而不使用此参数。 如果没有传入“display = page”参数,则默认为“display = touch”。 遗憾的是,此URL不会在URL中附加标记字符串,因此会显示第一个线程中显示的页面。

对此的解决方案是,不是使用下面的代码来生成登录URL,而是将其修改为

原版的:

Browser.Navigate(_fb.GetLoginUrl(parameters)); 

ammended:

 var URI = _fb.GetLoginUrl(parameters).toString().replace("www.facebook.com","m.facebook.com"); Browser.Navigate(new Uri(URI)); 

在我的项目中,我只是听了WebView的导航事件。 如果发生这种情况,则表示用户在登录页面上执行了某些操作(即按下登录按钮)。 然后我解析了你提到的应该包含OAuth回调url的页面的uri,如果它是正确的并且结果是成功的话我会手动重定向到正确的页面:

  //somewhere in the app private readonly FacebookClient _fb = new FacebookClient(); private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { FacebookOAuthResult oauthResult; if (!_fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult)) { return; } if (oauthResult.IsSuccess) { var accessToken = oauthResult.AccessToken; //you have an access token, you can proceed further FBLoginSucceded(accessToken); } else { // errors when logging in MessageBox.Show(oauthResult.ErrorDescription); } } 

如果您在异步函数中抽象日志记录,您希望它以异步方式运行,因此事件是可以的。

对不起我的英语不好。

整页的代码:

 public partial class LoginPageFacebook : PhoneApplicationPage { private readonly string AppId = Constants.FacebookAppId; private const string ExtendedPermissions = "user_birthday,email,user_photos"; private readonly FacebookClient _fb = new FacebookClient(); private Dictionary facebookData = new Dictionary(); UserIdentity userIdentity = App.Current.Resources["userIdentity"] as UserIdentity; public LoginPageFacebook() { InitializeComponent(); } private void webBrowser1_Loaded(object sender, RoutedEventArgs e) { var loginUrl = GetFacebookLoginUrl(AppId, ExtendedPermissions); webBrowser1.Navigate(loginUrl); } private Uri GetFacebookLoginUrl(string appId, string extendedPermissions) { var parameters = new Dictionary(); parameters["client_id"] = appId; parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html"; parameters["response_type"] = "token"; parameters["display"] = "touch"; // add the 'scope' only if we have extendedPermissions. if (!string.IsNullOrEmpty(extendedPermissions)) { // A comma-delimited list of permissions parameters["scope"] = extendedPermissions; } return _fb.GetLoginUrl(parameters); } private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { if (waitPanel.Visibility == Visibility.Visible) { waitPanel.Visibility = Visibility.Collapsed; webBrowser1.Visibility = Visibility.Visible; } FacebookOAuthResult oauthResult; if (!_fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult)) { return; } if (oauthResult.IsSuccess) { var accessToken = oauthResult.AccessToken; FBLoginSucceded(accessToken); } else { // user cancelled MessageBox.Show(oauthResult.ErrorDescription); } } private void FBLoginSucceded(string accessToken) { var fb = new FacebookClient(accessToken); fb.GetCompleted += (o, e) => { if (e.Error != null) { Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message)); return; } var result = (IDictionary)e.GetResultData(); var id = (string)result["id"]; userIdentity.FBAccessToken = accessToken; userIdentity.FBID = id; facebookData["Name"] = result["first_name"]; facebookData["Surname"] = result["last_name"]; facebookData["Email"] = result["email"]; facebookData["Birthday"] = DateTime.Parse((string)result["birthday"]); facebookData["Country"] = result["locale"]; Dispatcher.BeginInvoke(() => { BitmapImage profilePicture = new BitmapImage(new Uri(string.Format("https://graph.facebook.com/{0}/picture?type={1}&access_token={2}", id, "square", accessToken))); facebookData["ProfilePicture"] = profilePicture; userIdentity.FBData = facebookData; userIdentity.ProfilePicture = profilePicture; ARLoginOrRegister(); }); }; fb.GetAsync("me"); } private void ARLoginOrRegister() { WebService.ARServiceClient client = new WebService.ARServiceClient(); client.GetUserCompleted += client_GetUserCompleted; client.GetUserAsync((string)facebookData["Email"]); client.CloseAsync(); } void client_GetUserCompleted(object sender, WebService.GetUserCompletedEventArgs e) { if (e.Result == null) NavigationService.Navigate(new Uri("/RegisterPageFacebook.xaml", UriKind.RelativeOrAbsolute)); else if (e.Result.AccountType != (int)AccountType.Facebook) { MessageBox.Show("This account is not registered with facebook!"); NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.RelativeOrAbsolute)); } else { userIdentity.Authenticated += userIdentity_Authenticated; userIdentity.FetchARSocialData((string)facebookData["Email"]); } } void userIdentity_Authenticated(bool success) { NavigationService.Navigate(new Uri("/MenuPage.xaml", UriKind.RelativeOrAbsolute)); } } 

在尝试进行所有建议的更改之前,请检查您的Facebook应用程序是否处于沙盒模式。 这最终将解决您的问题。

在此处输入图像描述

如果您的Facebook应用程序处于沙盒模式,则只有开发人员可以使用他的电子邮件地址登录。 任何其他fb用户将获得白页。