我正在使用具有android和IOS项目的xamarin.forms PCL
以下是步骤:
我没有在我的iphone中使用facebook app登录。
当我尝试在facebook上注册时,safari打开,弹出消息打开"您已经授权APPName"
我点击OK按钮,然后再次弹出"在APPName"中打开页面。我点击确定按钮
我得到token = null和Iscancel = true;
以下是我的代码:
public class FacebookLoginButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
UIButton button = Control;
button.TouchUpInside += delegate
{
HandleFacebookLoginClicked();
};
}
}
private void HandleFacebookLoginClicked()
{
LoginManager manager = new LoginManager();
manager.Init();
manager.LogOut();
manager.LoginBehavior = LoginBehavior.SystemAccount;
manager.LogInWithReadPermissions(new string[] { "public_profile", "email", "user_birthday" }, (result, error) =>
{
if (error != null)
{
App.OnFacebookAuthFailed();
return;
}
else if (result.IsCancelled)
{
return;
}
else {
var UserToken = result.Token.TokenString;
if (UserToken != null)
{
var request = new Facebook.CoreKit.GraphRequest("/me?fields=id,email,first_name,last_name,gender,birthday", null, UserToken, null, "GET");
request.Start((connection, result1, error1) =>
{
if (error1 != null)
{
new UIAlertView("Error...", error.Description, null, "Ok", null).Show();
return;
}
var obj = result1 as NSDictionary;
FBModel fb = new FBModel();
fb.ID = obj["id"].ToString();
fb.Name = obj["first_name"].ToString();
fb.Email = obj["email"].ToString();
fb.LastName = obj["last_name"].ToString();
fb.Gender = obj["gender"].ToString();
if (obj.ContainsKey(new NSString("birthday")))
{
var birthdate = obj["birthday"].ToString();
fb.Age = DateTime.Today.Year - Convert.ToInt32(birthdate.Split('/')[2]);
}
else
{
fb.Age = 25;
}
App.OnFacebookIOSAuthSuccess(fb);
});
}
}
});
}
}
当我登录Facebook应用程序时,这段代码非常完美。
请建议我哪里出错了。 我怎样才能获得令牌?
答案 0 :(得分:0)
1)打开Safari浏览器
2)退出Facebook
3)打开应用程序并再次登录Facebook
private void HandleFacebookLoginClicked(){
LoginManager login = new LoginManager();
login.LogInWithReadPermissions(readPermissions.ToArray(),
delegate(LoginManagerLoginResult result, NSError error)
{
if (error != null)
{
App.OnFacebookAuthFailed();
}
else if (result.IsCancelled)
{
App.OnFacebookAuthFailed();
}
else
{
var accessToken = result.Token;
App.OnFacebookAuthSuccess(accessToken.TokenString);
}
});
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
FBSettings.DefaultAppID = FacebookAppId;
FBSettings.DefaultDisplayName = FacebookAppName;
return base.FinishedLaunching(app, options);
}
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
base.OpenUrl(application, url, sourceApplication, annotation);
return FBSession.ActiveSession.HandleOpenURL(url);
}
public override void OnActivated(UIApplication application)
{
base.OnActivated(application);
// We need to properly handle activation of the application with regards to SSO
// (e.g., returning from iOS 6.0 authorization dialog or from fast app switching).
FBSession.ActiveSession.HandleDidBecomeActive();
}
}