Xamarin.Auth OAuth2Authenticator Facebook NullReferenceException

I have created a function to login using Facebook.

public void Login ()
{
    var ctx = Forms.Context as MainActivity;
    var accounts = new List<Account>(AccountStore.Create (ctx).FindAccountsForService (SERVICE));
    if (accounts.Count == 1) {
        GetAccount (accounts [0]);
        return;
    }

    var auth = new OAuth2Authenticator (
        clientId: FBID,
        scope: string.Empty,
        authorizeUrl: new Uri (AUTH_URL),
        redirectUrl: new Uri (REDIRECT_URL));

    auth.Completed += (sender, eventArgs) => {
        AccountStore.Create (ctx).Save (eventArgs.Account, "Facebook");
        GetAccount (eventArgs.Account);
    };
    ctx.StartActivity (auth.GetUI (ctx));
}

The thing is that after I enter my credentials in the FB login page, an exception is thrown before it reaches the Completed event.
I've downloaded the Xamarin.Auth project from GitHub trying to debug the program, but it doesn't break at the breakpoint unfortunately.

Caused by: JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Auth.OAuth2Authenticator.OnRetrievedAccountProperties (System.Collections.Generic.IDictionary`2) [0x00017] in d:\Downloads\Xamarin.Auth-master\Xamarin.Auth-master\src\Xamarin.Auth\OAuth2Authenticator.cs:373
at Xamarin.Auth.OAuth2Authenticator.OnRedirectPageLoaded (System.Uri,System.Collections.Generic.IDictionary`2,System.Collections.Generic.IDictionary`2) [0x00016] in d:\Downloads\Xamarin.Auth-master\Xamarin.Auth-master\src\Xamarin.Auth\OAuth2Authenticator.cs:282
at Xamarin.Auth.WebRedirectAuthenticator.OnPageEncoun...[intentionally cut off]

I'm struggling with this issue for a while now. Please help!

Best Answer

Answers

  • BSalitaBSalita USMember

    I have a similar issue with a Xamarin.Auth app in development (VS 2013). When debugging, an exception is thrown. When launching on the device, the app works fine. I haven't found the culprit yet but am not blocked because I just launch on the device.

  • HansvandenElsenHansvandenElsen NLMember

    I've found it! It was a mix of circumstances.
    My debugger didn't stop at the breakpoints (don't know why).
    What caused the problem was that I created an Object with the Login method above in an OnCreate() method.
    Then I attached an EventHandler to an event of that Object.
    The moment the Authenticator comes back from his Intent, the Context where my object was bound is gone.
    It maybe is a bit vague to understand but maybe some code will clarify further.

    
    //Not working, causes the problem
    public class MyActivity {
        MyAuthenticator auth; //the object containing Login();
        public void OnCreate() {
            auth=new MyAuthenticator();
            auth.LoggedIn += blabla;
    
        }
        public void SomeMethod() {
            auth.Login();
        }
    }
    
    
    //Working, own scope
    public class MyActivity {
        public void OnCreate() {
            //ILB
        }
        public void SomeMethod() {
            var auth=new MyAuthenticator();
            auth.LoggedIn += blabla;
            auth.Login();
        }
    }
    
  • AravindhanPariAravindhanPari USMember

    Hi Hans van den Elsen,

    I'm a newbie to xamarin.forms and i was working on facebook & google plus login to my application. I have followed some of the links but none of them worked.You are provided only some part of the code so it was difficult for me to understand. Can you help me by providing the link which you followed ?

  • HansvandenElsenHansvandenElsen NLMember

    @AravindhanPari I've used the documentation of the Xamarin.Auth component. components.xamarin.com/view/xamarin.auth
    I've rewritten the DismissViewController thingy, which is iOS, and replaced it with Cross Platform code.
    Hope this answers your question.

  • AravindhanPariAravindhanPari USMember

    @Hans van den Elsen,

                Thanks for your reply. I already had a look into that link. But i was struck on creating a cross platform code instead of DismissViewController which will be used in ios alone. Frankly Still now i don't find any way to do that. Can you guide me on how you did that ?
    
Sign In or Register to comment.