OAuth2Authenticator - 对象引用未设置为对象的实例

OAuth2Authenticator - Object reference not set to an instance of an object

我有一个很简单的代码在ViewModel

private void _yOauth() 
{
    var auth = new OAuth2Authenticator
    (
        clientId: clientId,
        scope: scope,
        authorizeUrl: new Uri(oauthUrl),
        redirectUrl: new Uri(redirectUrl),
        clientSecret: clientSecret,
        accessTokenUrl: new Uri(accessTokenUrl)
    );

    auth.Completed += AuthOnCompleted;
    auth.Error += AuthOnError;

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

此代码在 presenter.Login(auth);

发生异常崩溃

Object reference not set to an instance of an object.

而且 presenterauth 不为空。

堆栈跟踪 :

at Xamarin.Auth.Presenters.OAuthLoginPresenter.Login (Xamarin.Auth.Authenticator authenticator) [0x00012] in <3887a0d7d292416b83829cf88b593985>:0 
  at Test.ViewModels.MainPageViewModel._yOauth () [0x00078] in D:\Test\Mobile\Test\ViewModels\MainPageViewModel.cs:107 
  at Test.ViewModels.MainPageViewModel.<.ctor>b__7_0 () [0x00001] in D:\MyUtilits\Test\Mobile\Test\ViewModels\MainPageViewModel.cs:29 
  at Xamarin.Forms.Command+<>c__DisplayClass4_0.<.ctor>b__0 (System.Object o) [0x00000] in D:\a\s\Xamarin.Forms.Core\Command.cs:74 
  at Xamarin.Forms.Command.Execute (System.Object parameter) [0x00000] in D:\a\s\Xamarin.Forms.Core\Command.cs:112 
  at Xamarin.Forms.ButtonElement.ElementClicked (Xamarin.Forms.VisualElement visualElement, Xamarin.Forms.Internals.IButtonElement ButtonElementManager) [0x00008] in D:\a\s\Xamarin.Forms.Core\ButtonElement.cs:60 
  at Xamarin.Forms.Button.SendClicked () [0x00000] in D:\a\s\Xamarin.Forms.Core\Button.cs:173 
  at Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick (Xamarin.Forms.VisualElement element, Xamarin.Forms.IButtonController buttonController, Android.Views.View v) [0x00000] in D:\a\s\Xamarin.Forms.Platform.Android\ButtonElementManager.cs:25 
  at Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick (Android.Views.View v) [0x00000] in D:\a\s\Xamarin.Forms.Platform.Android\FastRenderers\ButtonRenderer.cs:72 
  at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00010] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Android.Views.View.cs:2230 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.43(intptr,intptr,intptr)

我遇到了类似的问题,我所要做的就是在 Android Project

中添加这一行
global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, savedInstanceState);

所以它看起来像这样:

...
protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, savedInstanceState);

            LoadApplication(new App());
        }
...

不确定,但是对于 iOS 和其他平台,您可能需要类似的东西。请参阅 https://github.com/moljac/Xamarin.Auth.Samples.NugetReferences/ 对于一些样本...