Vipin Krishna
Vipin Krishna

Reputation: 365

Java Lang Illegal State Exception for GoogleClientManager Logout in Xamarin Forms

I'm using https://www.pujolsluis.com/google-client-plugin-for-xamarin/ for google sign in xamarin forms. Both Login and Logout methods work fine; but i have to hide the login page after successful login.After opening the app from second time onwards, logout method throws java.Lang.IlegalStateException<Timeout exceeded getting exception details> ,cannot logout.Active token is null.How to handle this exception? How to logout successfully from second time?

Login:

public IGoogleClientManager googleClientManager;
googleClientManager = CrossGoogleClient.Current;

    private void google_btn_Clicked(object sender, EventArgs e)
    {
        if (CrossConnectivity.Current.IsConnected)
        {
            googleClientManager.LoginAsync();
            googleClientManager.OnLogin += OnLoginCompleted;
            //   CrossGoogleClient.Current.SilentLoginAsync();
            //   var userToken = CrossGoogleClient.Current.ActiveToken;
        }
        else
        {
            DependencyService.Get<IToast>().LongAlert("Check  Connection!");
        }
    }

    public async void OnLoginCompleted(object s,
    GoogleClientResultEventArgs<GoogleUser> loginEventArgs)
    {           
        if (loginEventArgs.Data != null)
        {
            GoogleUser googleUser = loginEventArgs.Data;                 
            string google_name = googleUser.Name;               
            string google_mail = googleUser.Email;        
             Uri google_img = googleUser.Picture;                
            googleClientManager.OnLogin -= OnLoginCompleted;                                    
        }                                       
    }

Logout:

    public void Logout()
    {
        googleClientManager.OnLogout += OnLogoutCompleted;
        googleClientManager.Logout(); // throws exception from secondtime after hiding loginpage
    }

    private void OnLogoutCompleted(object sender, EventArgs loginEventArgs)
    {
        googleClientManager.OnLogout -= OnLogoutCompleted;
    }

Upvotes: 0

Views: 329

Answers (1)

Pujolsluis
Pujolsluis

Reputation: 48

You are getting this exception because you are trying to log out of a google client that is no longer connected as the message of the exception states.

Google Client Illegal State Exception Screenshot

To solve this you could do two things, fix the logic in your app to persist the logout state, so you don't try and log out when the user is not actually logged in anymore. Or you could enable the ActiveToken and add an if statement before trying to log out to verify if it's null or not you can do so by following the steps on the getting started guide on the project repo: https://github.com/CrossGeeks/GoogleClientPlugin/blob/master/GoogleClient/docs/GettingStarted.md

Activate ActiveToken Google Client Plugin Guide Screenshot

Upvotes: 1

Related Questions