Forum Libraries, Components, and Plugins

Xamarin.Auth alway throws Exception on Android

The Exception is throwed as follows:

Activity xamarin.auth.WebAuthenticatorActivity has leaked window [email protected] that was originally added here
android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?

Work flow of Xamarin.Auth is as follows (I debugged inside the Xamarin.Auth source code):
1 -> You create an instance of Oauth2Authenticator, that will create an activty with a Webview inside. The Webview will open the Facebook login page.
2 -> You type login information and press Login.
3 -> You manually handle Completed event which fired by Xamarin.Auth if the login information was correct.
4 -> Xamarin.Auth finishs the activity.
5 -> Xamarin.Auth make a request to redirect_uri which was passed to Oauth2Authenticator.

As everyone can see, the step 4 & 5 is crazy. The author of Xamarin.Auth should fix this problem.

Posts

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    I ran into this recently. Do you have "NoHistory=true"? After I removed this it worked just fine for me. It most likely had to do with Android's activity lifecycle because once you navigate away you can't get back to it.

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai
    edited January 2014

    So I kept getting this randomly and on different versions of Android. I cloned Xamarin.Auth and commented what is inside of ActivityEx.cs's method "ShowError". I still need to research more, but that cleared it up for me. I will add an issue on it.

    I should say that this is probably not the real issue though, I would have to debug more and see why ShowError is being called and that is the real issue.

  • James - any further update on this?

    One of my testers is getting this same error that I can't reproduce. It looks like it's happening when I make a call to Google's https://www.googleapis.com/plus/v1/people/me/openIdConnect, so the initial Google login has completed successfully.

  • By adding a hook into the .Error method of the OAuth2Authenticator I can see that I am consistently getting this error when running on Android 2.3.3 when trying to make the openIdConnect call. The .Completed method is firing, so it looks like the initial Google login is good.

    Expected access_token in response, but did not receive one.

    It's working on KitKat

  • Sreeraj.P.RSreeraj.P.R USMember

    Hi,
    I am trying to get authorized to access LinkedIn API. I could not figure out how to give a redirect url to my android app. Can anyone help me on what to provide as redirect url ?
    Thank You

  • I was still getting this so decided to include the Xamarin.Auth.Android source in my solution as a project rather than linked DLL and 'have a hack'.

    What I've found to get it to work on both 2.3 and 4.4 Android:

    1. The OP issue is caused (as James points out) by an Alert in ActivityEx.cs. My hack for this - wrap it in a try/catch and just dump the output to Android log if there's an error.

      public static void ShowError (this Activity activity, string title, string message)
      {
          try {
              var b = new AlertDialog.Builder (activity);
              b.SetMessage (message);
              b.SetTitle (title);
              b.SetNeutralButton ("OK", (s, e) => {
                  ((AlertDialog)s).Cancel ();
              });
              var alert = b.Create ();
              alert.Show ();
          } catch (Exception ex) {
              Log.Error("Xamarin", "Error: {0}:{1}", title, message);
          }
      }
      
    2. My problem with Android 2.3 authentication failing with an "Expected access_token in response, but did not receive one." message. This seems to be caused by some of the redirect handling in WebRedirectAuthenticator.cs. On an initial call, the fragment parameter is nicely populated with access_token et al, but a secondary call is a plain Uri without the parameters. This overwrites the initial set and causes the error. My hack - simply ignore any calls that don't have the full fragment set expected.

      protected virtual void OnPageEncountered (Uri url, IDictionary<string, string> query, IDictionary<string, string> fragment)
      {
          var all = new Dictionary<string, string> (query);
          foreach (var kv in fragment)
              all [kv.Key] = kv.Value;
      
          //
          // Check for errors
          //
          if (all.ContainsKey ("error")) {
              var description = all ["error"];
              if (all.ContainsKey ("error_description")) {
                  description = all ["error_description"];
              }
              OnError (description);
              return;
          }
      
          //
          // Watch for the redirect
          //
          if (UrlMatchesRedirect (url)) {
              if (fragment.Count > 1)  // HACK: Jens's hack to fix my authentication for Android 2.3 devices
                  OnRedirectPageLoaded (url, query, fragment);
          }
      }
      

    Really cheap hacks, but for want of better understanding the reason behind the redirect hiding, it "works for me"! :-)

  • Some additional information. I updated my Authentication module to use one directly linked from the store rather than a downloaded one, not sure if it's updated or not. Here is the log from the latest crash from the user.

    He has a Samsung Galaxy SII, witnesses the crash. I logged on with his credentials (he's a relative and trusts me!) on my Note II and have no crash, so it's not account related. I'm running an AOSP custom KitKat ROM, not sure yet what version he has.

    03-19 10:52:33.798 I/MonoDroid(27499): UNHANDLED EXCEPTION: Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
    03-19 10:52:33.798 I/MonoDroid(27499): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) <0x00118>
    03-19 10:52:33.798 I/MonoDroid(27499): at Android.App.Dialog.Show () <0x000a7>
    03-19 10:52:33.798 I/MonoDroid(27499): at Xamarin.Utilities.Android.ActivityEx.ShowError (Android.App.Activity,string,string) <0x00117>
    03-19 10:52:33.798 I/MonoDroid(27499): at Xamarin.Auth.WebAuthenticatorActivity.<OnCreate>b__2 (object,Xamarin.Auth.AuthenticatorErrorEventArgs) <0x00057>
    03-19 10:52:33.798 I/MonoDroid(27499): at Xamarin.Auth.Authenticator/<>c__DisplayClass5.<OnError>b__4 () <0x0006b>
    03-19 10:52:33.798 I/MonoDroid(27499): at Java.Lang.Thread/RunnableImplementor.Run () <0x0003f>
    03-19 10:52:33.798 I/MonoDroid(27499): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <0x0003b>
    03-19 10:52:33.798 I/MonoDroid(27499): at (wrapper dynamic-method) object.84094b72-e546-4d69-a1da-7cc1379f019a (intptr,intptr) <0x0003b>
    03-19 10:52:33.798 I/MonoDroid(27499): 
    03-19 10:52:33.798 I/MonoDroid(27499):   --- End of managed exception stack trace ---
    03-19 10:52:33.798 I/MonoDroid(27499): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.ViewRoot.setView(ViewRoot.java:538)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:203)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.app.Dialog.show(Dialog.java:241)
    03-19 10:52:33.798 I/MonoDroid(27499):  at mono.java.lang.RunnableImplementor.n_run(Native Method)
    03-19 10:52:33.798 I/MonoDroid(27499):  at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.app.Activity.runOnUiThread(Activity.java:3759)
    03-19 10:52:33.798 I/MonoDroid(27499):  at xamarin.auth.WebAuthenticatorActivity_Client.n_onPageStarted(Native Method)
    03-19 10:52:33.798 I/MonoDroid(27499):  at xamarin.auth.WebAuthenticatorActivity_Client.onPageStarted(WebAuthenticatorActivity_Client.java:46)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:273)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.os.Handler.dispatchMessage(Handler.java:99)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.os.Looper.loop(Looper.java:130)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.app.ActivityThread.main(ActivityThread.java:3691)
    03-19 10:52:33.798 I/MonoDroid(27499):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-19 10:52:33.798 I/MonoDroid(27499):  at java.lang.reflect.Method.invoke(Method.java:507)
    03-19 10:52:33.798 I/MonoDroid(27499):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    03-19 10:52:33.798 I/MonoDroid(27499):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    03-19 10:52:33.798 I/MonoDroid(27499):  at dalvik.system.NativeStart.main(Native Method)
    

    The crash seems to occur on making a call to

    var request = new OAuth2Request("GET", new Uri("https://www.googleapis.com/plus/v1/people/me/openIdConnect"), null, e.Account);
    
  • Some additional information. I updated my Authentication module to use one directly linked from the store rather than a downloaded one, not sure if it's updated or not. Here is the log from the latest crash from the user.

    He has a Samsung Galaxy SII, witnesses the crash. I logged on with his credentials (he's a relative and trusts me!) on my Note II and have no crash, so it's not account related. I'm running an AOSP custom KitKat ROM, not sure yet what version he has.

    03-19 10:52:33.798 I/MonoDroid(27499): UNHANDLED EXCEPTION: Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
    03-19 10:52:33.798 I/MonoDroid(27499): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) <0x00118>
    03-19 10:52:33.798 I/MonoDroid(27499): at Android.App.Dialog.Show () <0x000a7>
    03-19 10:52:33.798 I/MonoDroid(27499): at Xamarin.Utilities.Android.ActivityEx.ShowError (Android.App.Activity,string,string) <0x00117>
    03-19 10:52:33.798 I/MonoDroid(27499): at Xamarin.Auth.WebAuthenticatorActivity.<OnCreate>b__2 (object,Xamarin.Auth.AuthenticatorErrorEventArgs) <0x00057>
    03-19 10:52:33.798 I/MonoDroid(27499): at Xamarin.Auth.Authenticator/<>c__DisplayClass5.<OnError>b__4 () <0x0006b>
    03-19 10:52:33.798 I/MonoDroid(27499): at Java.Lang.Thread/RunnableImplementor.Run () <0x0003f>
    03-19 10:52:33.798 I/MonoDroid(27499): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <0x0003b>
    03-19 10:52:33.798 I/MonoDroid(27499): at (wrapper dynamic-method) object.84094b72-e546-4d69-a1da-7cc1379f019a (intptr,intptr) <0x0003b>
    03-19 10:52:33.798 I/MonoDroid(27499): 
    03-19 10:52:33.798 I/MonoDroid(27499):   --- End of managed exception stack trace ---
    03-19 10:52:33.798 I/MonoDroid(27499): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.ViewRoot.setView(ViewRoot.java:538)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:203)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.app.Dialog.show(Dialog.java:241)
    03-19 10:52:33.798 I/MonoDroid(27499):  at mono.java.lang.RunnableImplementor.n_run(Native Method)
    03-19 10:52:33.798 I/MonoDroid(27499):  at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.app.Activity.runOnUiThread(Activity.java:3759)
    03-19 10:52:33.798 I/MonoDroid(27499):  at xamarin.auth.WebAuthenticatorActivity_Client.n_onPageStarted(Native Method)
    03-19 10:52:33.798 I/MonoDroid(27499):  at xamarin.auth.WebAuthenticatorActivity_Client.onPageStarted(WebAuthenticatorActivity_Client.java:46)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:273)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.os.Handler.dispatchMessage(Handler.java:99)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.os.Looper.loop(Looper.java:130)
    03-19 10:52:33.798 I/MonoDroid(27499):  at android.app.ActivityThread.main(ActivityThread.java:3691)
    03-19 10:52:33.798 I/MonoDroid(27499):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-19 10:52:33.798 I/MonoDroid(27499):  at java.lang.reflect.Method.invoke(Method.java:507)
    03-19 10:52:33.798 I/MonoDroid(27499):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    03-19 10:52:33.798 I/MonoDroid(27499):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    03-19 10:52:33.798 I/MonoDroid(27499):  at dalvik.system.NativeStart.main(Native Method)
    

    The crash seems to occur on making a call to

    var request = new OAuth2Request("GET", new Uri("https://www.googleapis.com/plus/v1/people/me/openIdConnect"), null, e.Account);
    
  • ADeCraemerADeCraemer USMember ✭✭

    Any update on this?

  • Bump, has anyone found a solution other than including the source for the Auth library?

  • snehthaparsnehthapar USMember

    add this tag on your activity. NoHistory=false

  • JakubHolovskyJakubHolovsky CZMember

    @snehthapar said:
    add this tag on your activity. NoHistory=false

    Doesn't solve my problem at all. I am not using that in my activity.

  • for anyone who is still have this error and want for now to avoid it, use this:
    auth.ShowUIErrors = false;

  • PD.6026PD.6026 USMember ✭✭

    Does anybody has any solutions for this? I am using it with my Xamarin Forms project. It's an old project. Everything was well until I found this issue yesterday. With auth.ShowUIErrors = false; also its not working. The error is not consistent. Sometimes it runs well and sometimes it crashes the app. Can anybody please help.

    Thanks

  • PD.6026PD.6026 USMember ✭✭

    Well, got it resolved. If anybody need here is what I did-
    Download the project from https://github.com/xamarin/Xamarin.Auth . Open the project Xamarin.Auth.Android.
    In the ActivityEx.cs file modify the following line of code-
    `public static void ShowError (this Activity activity, string title, string message)
    {
    try {
    var b = new AlertDialog.Builder (activity);
    b.SetMessage (message);
    b.SetTitle (title);
    b.SetNeutralButton ("OK", (s, e) => {
    ((AlertDialog)s).Cancel ();
    });
    var alert = b.Create ();
    alert.Show();
    }
    catch(Exception ex) { }

        }`
    

    In the file WebAuthenticatorActivity.cs modify the following code for OnReceivedSslError() in last line

    try { builder.Create ().Show (); } catch { }

    This resolved the issue.

    Thanks

Sign In or Register to comment.