【问题标题】:firebase google authentication in web does not work on android app webview网络中的firebase google身份验证不适用于android应用程序webview
【发布时间】:2019-07-06 15:45:08
【问题描述】:

我正在创建一个使用 firebase google 身份验证登录的网站。它在所有浏览器中都可以正常工作。但是当我在我的应用程序中将此网站添加为 webview 时,它不起作用。

显示此错误的网站:

运行此应用程序的环境不支持此操作。 “location.protocol”必须是 http、https 或 chrome-extension,并且必须启用网络存储。

下面是一些代码:

javascript 代码:

function login(){
    console.log('login called');
    function newLoginHappend(user){
        if(user){
            model_questions(user);
        }else{
            var provider = new firebase.auth.GoogleAuthProvider();

            firebase.auth().signInWithPopup(provider).then(function(result) {
              // This gives you a Google Access Token. You can use it to access the Google API.
              var token = result.credential.accessToken;
              // The signed-in user info.
              var user = result.user;
              // ...
            }).catch(function(error) {
              // Handle Errors here.
              var errorCode = error.code;
              var errorMessage = error.message;
              // The email of the user's account used.
              var email = error.email;
              // The firebase.auth.AuthCredential type that was used.
              var credential = error.credential;
              // ...
            });
        }
    }

    firebase.auth().onAuthStateChanged(newLoginHappend);
}

window.onload = login();

网页浏览代码:

   WebSettings webSettings =webView.getSettings();
   webSettings.setJavaScriptEnabled(true);
   webView.setWebViewClient(new WebViewClient());
   webView.loadUrl("https://mahmud-cse16.github.io/CBAP_Handout/");

有什么方法或技术可以解决这个问题吗?如果您有任何想法,请与我们分享。

谢谢

【问题讨论】:

  • 启用DOM存储并检查问题是否仍然存在,webSettings.setDomStorageEnabled(true);
  • 谢谢它的工作原理。尝试此操作后,我收到了 disallowed_useragent 错误。然后我就修好了。

标签: javascript android firebase-authentication android-webview


【解决方案1】:

尝试为 webview 启用 DOM 存储

WebSettings webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true);   // localStorage

设置是否启用 DOM 存储 API。默认值为 false。

Android Developer Reference

【讨论】:

    【解决方案2】:

    要绕过“disallowed_useragent”错误,一种方法是使用 Android Google Auth SDK 在应用程序中本地登录,然后将 Google 令牌传递给 Webview,以便 Firebase 可以将其与 auth.signInWithCredential() 一起使用。

    它可能适用于其他提供商,但这是我在 Android 上使用 Google 身份验证的方法:

    安卓:

    val gso =
        GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
          .requestIdToken(clientId)
          .requestEmail()
          .build()
    val googleSignInClient = GoogleSignIn.getClient(activity, gso)
    
    activity.startActivityForResult(
            googleSignInClient.signInIntent,
            object : ActivityWithResultListener.OnActivityResultListener {
              override fun onActivityResult(resultCode: Int, data: Intent?) {
                if (data != null) {
                  val credential = GoogleAuthProvider.getCredential(account.idToken!!, null)
                  idToken = account.idToken!!
    
                  // Then pass the token to your webview via a JS interface
                }
              }
           }
    )
    
    
    

    网页:

    const idTokenFromApp = AndroidBridge.getAuthToken(); // This is the idToken from the Android code above
    if (idTokenFromApp) {
        // from https://firebase.google.com/docs/auth/web/google-signin
        // Build Firebase credential with the Google ID token.
        // https://firebase.google.com/docs/reference/js/v8/firebase.auth.GoogleAuthProvider#static-credential
        const credential = firebase.auth.GoogleAuthProvider.credential(idTokenFromApp);
    
        // Sign in with credential from the Google user.
        firebase.auth.signInWithCredential(credential).catch((error) => {
          // Handle Errors here.
          const errorCode = error.code;
          const errorMessage = error.message;
    
          logError(`Error logging in: ${errorCode} ${errorMessage} token: ${idTokenFromApp}`, error);
        });
    }
    
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 2017-04-15
      • 2018-01-26
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      相关资源
      最近更新 更多