C# WP8-Facebook登录问题
我正在尝试使用WindowsPhone8上的Facebook C#SDK对Facebook上的用户进行身份验证。为此,我遵循以下代码: 但我面临的问题是,每当我在打开的对话框中输入用户名和密码以验证用户身份时,我只会看到以下页面: 在此之后,我的程序不会重定向到作为单独视图的登录页。我看到的其他建议隐藏WebView的解决方案不适用,因为身份验证被抽象为单个LoginAsync函数调用。C# WP8-Facebook登录问题,c#,facebook,windows-phone-8,facebook-c#-sdk,C#,Facebook,Windows Phone 8,Facebook C# Sdk,我正在尝试使用WindowsPhone8上的Facebook C#SDK对Facebook上的用户进行身份验证。为此,我遵循以下代码: 但我面临的问题是,每当我在打开的对话框中输入用户名和密码以验证用户身份时,我只会看到以下页面: 在此之后,我的程序不会重定向到作为单独视图的登录页。我看到的其他建议隐藏WebView的解决方案不适用,因为身份验证被抽象为单个LoginAsync函数调用。 有什么建议吗?在我的项目中,我刚刚听了WebView的导航事件。如果发生这种情况,则表示用户在登录页面上做
有什么建议吗?在我的项目中,我刚刚听了WebView的导航事件。如果发生这种情况,则表示用户在登录页面上做了一些事情(即按下了登录按钮)。 然后我解析了您提到的页面的uri,该页面应该包含OAuth回调url,如果正确并且结果是成功,我将手动重定向到正确的页面:
//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<string, object> facebookData = new Dictionary<string, object>();
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<string, object>();
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<string, object>)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:PhoneApplicationPage
{
私有只读字符串AppId=Constants.FacebookAppId;
private const string extendedpowpermissions=“用户\生日、电子邮件、用户\照片”;
私有只读FacebookClient _fb=新FacebookClient();
专用字典facebookData=新字典();
UserIdentity UserIdentity=App.Current.Resources[“UserIdentity”]作为UserIdentity;
公共登录Facebook()
{
初始化组件();
}
已加载私有void webBrowser1_(对象发送器,路由目标e)
{
var loginUrl=GetFacebookLoginUrl(AppId,ExtendedPermissions);
webBrowser1.导航(loginUrl);
}
私有Uri GetFacebookLoginUrl(字符串appId,字符串extendedPermissions)
{
var参数=新字典();
参数[“客户端id”]=appId;
参数[“重定向uri”]=”https://www.facebook.com/connect/login_success.html";
参数[“响应类型”]=“令牌”;
参数[“显示”]=“触摸”;
//仅当我们具有extendedPermissions时才添加“范围”。
如果(!string.IsNullOrEmpty(extendedPermissions))
{
//以逗号分隔的权限列表
参数[“范围”]=扩展权限;
}
返回_fb.GetLoginUrl(参数);
}
私有void webBrowser1_已导航(对象发送方,System.Windows.Navigation.NavigationEventArgs e)
{
if(waitPanel.Visibility==Visibility.Visibility)
{
waitPanel.Visibility=Visibility.collazed;
webBrowser1.Visibility=可见性.Visibility;
}
FacebookOAuthResult oauthResult;
if(!\u fb.TryParseOAuthCallbackUrl(e.Uri,out oauthResult))
{
返回;
}
if(oauthResult.IsSuccess)
{
var accessToken=oauthResult.accessToken;
FBLoginSAccessed(accessToken);
}
其他的
{
//用户取消
MessageBox.Show(oauthResult.ErrorDescription);
}
}
私有void FBLoginSAccessed(字符串accessToken)
{
var fb=新的FacebookClient(accessToken);
fb.GetCompleted+=(o,e)=>
{
如果(例如错误!=null)
{
Dispatcher.BeginInvoke(()=>MessageBox.Show(e.Error.Message));
返回;
}
var result=(IDictionary)e.GetResultData();
变量id=(字符串)结果[“id”];
userIdentity.FBAccessToken=accessToken;
userIdentity.FBID=id;
facebookData[“Name”]=结果[“first_Name”];
facebookData[“姓氏”]=结果[“姓氏”];
facebookData[“电子邮件”]=结果[“电子邮件”];
facebook数据[“生日”]=DateTime.Parse((字符串)结果[“生日”]);
facebookData[“国家”]=结果[“地区”];
Dispatcher.BeginInvoke(()=>
{
BitmapImage profilePicture=新的BitmapImage(新Uri(string.Format()https://graph.facebook.com/{0}/picture?type={1}&access_-token={2},id,“square”,accessToken));
facebookData[“ProfilePicture”]=ProfilePicture;
userIdentity.FBData=facebook数据;
userIdentity.ProfilePicture=ProfilePicture;
ARLoginOrRegister();
});
};
fb.GetAsync(“我”);
}
私有void ARLoginOrRegister()
{
WebService.ARServiceClient=新的WebService.ARServiceClient();
client.GetUserCompleted+=客户端_GetUserCompleted;
GetUserAsync((字符串)facebookData[“Email”]);
client.CloseAsync();
}
无效客户端\u GetUserCompleted(对象发送方,WebService.GetUserCompletedEventArgs e)
{
如果(e.Result==null)
NavigationService.Navigate(新Uri(“/RegisterPageFacebook.xaml”,UriKind.RelativeOrAbsolute));
else if(e.Result.AccountType!=(int)AccountType.Facebook)
{
Show(“此帐户未在facebook注册!”);
NavigationService.Navigate(新Uri(“/LoginPage.xaml”,UriKind.RelativeOrAbsolute));
}
其他的
{
userIdentity.Authenticated+=userIdentity\u Authenticated;
FetchARSocialData((字符串)facebook数据[“Email”]);
}
}
无效用户身份验证(bool成功)
{
NavigationService.Navigate(新Uri(“/MenuPage.xaml”,UriKind.RelativeOrAbsolute));
}
}
当FB检测到Windows Phone网络浏览器控件时,它似乎对其重定向脚本进行了一些更改
C#SDK所做的是将登录页面生成为“…”。当您在webbrowser控件上打开此URL时,它将被重定向到“…”,其中显示FB的移动版本
Browser.Navigate(_fb.GetLoginUrl(parameters));
var URI = _fb.GetLoginUrl(parameters).toString().replace("www.facebook.com","m.facebook.com");
Browser.Navigate(new Uri(URI));