首页 > 解决方案 > Xamarin 表单中 GoogleClientManager 注销的 Java 语言非法状态异常

问题描述

我正在使用https://www.pujolsluis.com/google-client-plugin-for-xamarin/进行谷歌登录 xamarin 表单。登录和注销方法都可以正常工作;但是我必须在成功登录后隐藏登录页面。从第二次开始打开应用程序后,注销方法抛出java.Lang.IlegalStateException<Timeout exceeded getting exception details>,无法注销。活动令牌为空。如何处理此异常?如何从第二次成功注销?

登录:

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;                                    
        }                                       
    }

登出:

    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;
    }

标签: xamarinxamarin.formsillegalstateexceptiongoogle-signin

解决方案


您收到此异常是因为您尝试注销不再连接的谷歌客户端,因为异常状态的消息。

谷歌客户端非法状态异常截图

要解决这个问题,您可以做两件事,修复应用程序中的逻辑以保持注销状态,这样您就不会在用户实际上不再登录时尝试注销。或者,您可以启用 ActiveToken 并在尝试注销之前添加一个 if 语句以验证它是否为空,您可以按照项目 repo 的入门指南中的步骤进行操作:https ://github.com/CrossGeeks /GoogleClientPlugin/blob/master/GoogleClient/docs/GettingStarted.md

激活 ActiveToken 谷歌客户端插件指南截图


推荐阅读