首页 > 解决方案 > 我可以使用 Android WebView 对象进行 Google 身份验证吗?

问题描述

我正在使用 Android Studio 更新通过 Google 进行身份验证的登录页面。在 onCreate 方法中,我有一个名为 loadWebViewPage() 的函数,它加载我们使用 Google 进行身份验证的身份验证页面。当调用 loadWebViewPage() 时,我有以下内容:

private void loadWebViewPage()
{
    
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    String userAgent = "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Mobile Safari/535.19";
    //Set WebView
    WebView webView =(WebView)findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setUserAgentString(userAgent);
    //Set WebSettings
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    {
     webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    


    webView.loadUrl("the url that redirects to sso page");

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    LocalBroadcastManager.getInstance(getApplicationContext())
            .registerReceiver(loginBroadcastReceiver,
                    new IntentFilter(LoginHelper.LOGIN_MESSAGE));

}

onCreate 方法工作正常;我让页面加载,用户可以输入他们的电子邮件和密码。但是,当用户单击登录按钮时,它不想加载 Google 身份验证,他们应该能够选择他们想要登录的 Google 帐户。

我尝试寻找解决方案,但我看到的是用户代理或 Google 不再支持 Webviews,我需要设置一个处理身份验证的浏览器,然后在成功登录后返回应用程序。

我认为最简单的解决方案是更新 User-Agent 变量。但是,我是否遗漏了什么,或者应该以不同的方式处理这个问题?我不想加载实际的浏览器,因为我们不希望用户弄乱 URL 地址或担心导航按钮。

谢谢

标签: androidwebviewgoogle-authentication

解决方案


推荐阅读