使用Xamarin.Auth通过Facebook进行身份验证

时间:2016-02-02 13:24:39

标签: xamarin facebook-login xamarin.auth

我正在使用Xamarin.Auth在我的应用程序(android / iOS)中登录facebook并且一切顺利但是当成功进入时,facebook个人资料正在打开而不会回到我的应用程序。我想重定向到我的应用程序的主页,而不显示Facebook个人资料。我遵循这个tutorial并没有取得任何成功。我想我没有给我的应用程序正确的Urls。请给我一些建议。 我们将提前感谢您的帮助。

这是我的loginPageRenderer代码:

[assembly: ExportRenderer (typeof (FBLoginPage), typeof (LoginPageRendrerr))]
 namespace FFirst_app.Droid
 {
public class LoginPageRendrerr : PageRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        base.OnElementChanged(e);

        // this is a ViewGroup - so should be able to load an AXML file and FindView<>
        var activity = this.Context as Activity;

        var auth = new OAuth2Authenticator (
            clientId: "7b745e26dbb64e1a3a3bf6bfd33165bc", // your OAuth2 client id
            scope: "basic", // the scopes for the particular API you're accessing, delimited by "+" symbols
            authorizeUrl: new Uri("https://apps.facebook.com/myappppppp"),//("https://api.instagram.com/oauth/authorize/"), // the auth URL for the service
            redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html")); // the redirect URL for the service

        auth.Completed += (sender, eventArgs) => {
            if (eventArgs.IsAuthenticated) 
            {
                App.SuccessfulLoginAction.Invoke();

                // Use eventArgs.Account to do wonderful things
                App.SaveToken(eventArgs.Account.Properties["access_token"]);

                string sessionToken = App.Token;   //  /* Authenticate the user with Facebook and fetch a session token */;
                DateTime expiration =  DateTime.Today;  ///* The expiration time for the session token */;
                string facebookId = Constants.FBAppId;
                 ParseFacebookUtils.LogInAsync (facebookId, sessionToken, expiration);

            } else {
                // The user cancelled
            }



        };

        activity.StartActivity (auth.GetUI(activity));
    }
}

 }

2 个答案:

答案 0 :(得分:1)

我已经找到了答案,我的URL错了,现在它的工作在我将'authorizeUrl:'改为“http://www.facebook.com/connect/login_success.html”之后,这个URl也在FB App上设置了。所以现在我的代码是工作正常。

答案 1 :(得分:0)

Xamarin.Auth Getting Started页面上,提到您可以解雇iOS上的用户界面:

auth.Completed += (sender, eventArgs) => {

    // We presented the UI, so it's up to us to dimiss it on iOS.
    **> DismissViewController (true, null); <**

    if (eventArgs.IsAuthenticated) {
        // Use eventArgs.Account to do wonderful things
    } else {
        // The user cancelled
    }
};