5

I am following This Page along with some samples from Xamarin.Auth github repo so set up a facebook login. I have created the following login function:

public void Authenticate()
{
    string clientId = Config.FacebookAppID;
    string redirectUri = redirectUri = "MyApp:/authorize";
    var authenticator = new OAuth2Authenticator(
        clientId: clientId,
        scope: "public_profile,email",
        authorizeUrl: new Uri("https://www.facebook.com/v2.9/dialog/oauth"),
        redirectUrl: new Uri(redirectUri),
        getUsernameAsync: null,
        isUsingNativeUI: true);

    authenticator.AllowCancel = true;
    authenticator.Completed += OnAuthCompleted;
    authenticator.Error += OnAuthError;
    Authenticator = authenticator;

    var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
    presenter.Login(Authenticator);
}

But I am getting the following exception on the last line:

{System.NullReferenceException: Object reference not set to an instance of an object.
  at Xamarin.Auth.Presenters.OAuthLoginPresenter.Login (Xamarin.Auth.Authenticator authenticator) [0x00011] in C:\cxa\source\Xamarin.Auth.LinkSource\Request.cs:290 
  at MyApp.Components.AuthorisationManager.Authenticate () [0x00078] in D:\Dev\MenuSystem.AdminApp\MyApp\MyApp\MyApp\Components\AuthorisationManager.cs:56 
  at MyApp.ViewModels.MainPageViewModel.NavigateToMenuItem () [0x00008] in D:\Dev\MenuSystem.AdminApp\MyApp\MyApp\MyApp\ViewModels\MainPageViewModel.cs:63 }

Any ideas what I may be doing wrong?

Axel
  • 3,331
  • 11
  • 35
  • 58
Kayes
  • 502
  • 7
  • 18

1 Answers1

2

I know this is over a year old, however, I ran into this same exact issue following a guided linked-in training.

The answer is to initialize the AuthenticationConfiguration in the FinishedLaunching method in the AppDelegate class.

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    // Override point for customization after application launch.
    // If not required for your application you can safely delete this method
    // Add the Init() method here
    global::Xamarin.Auth.Presenters.XamarinIOS.AuthenticationConfiguration.Init();
    return true;
}
Jake1164
  • 12,291
  • 6
  • 47
  • 64
  • Thank you Jake for the answer. I'd stopped working on xamarin ;), but surprised that the issue still exists! I wonder why the tutorials we are following don't get this issue. Nevertheless, I've marked it as answer as it'll definitely help someone else. – Kayes Nov 07 '18 at 00:29