firebase google web 中的身份验证不适用于 android app webview
firebase google authentication in web does not work on android app webview
我正在创建一个使用 firebase google 身份验证登录的网站。它在所有浏览器中都运行良好。但是当我在我的应用程序中添加这个网站作为 webview 它不起作用。
显示此错误的网站:
This operation is not supported in the environment this application is running on. "location.protocol" must be http, https or chrome-extension and web storage must be enabled.
下面是一些代码:
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 webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true); // localStorage
设置是否启用DOM存储API。默认值为 false。
要解决“disallowed_useragent”错误,一种方法是使用 Android Google Auth SDK 在应用程序中本地登录,然后通过 Google 标记到 Webview,以便 Firebase 可以将其与 auth.signInWithCredential()
.
一起使用
它可能适用于其他提供商,但这是我在 Android 上使用 Google 身份验证的方式:
Android:
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);
});
}
我正在创建一个使用 firebase google 身份验证登录的网站。它在所有浏览器中都运行良好。但是当我在我的应用程序中添加这个网站作为 webview 它不起作用。
显示此错误的网站:
This operation is not supported in the environment this application is running on. "location.protocol" must be http, https or chrome-extension and web storage must be enabled.
下面是一些代码:
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 webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true); // localStorage
设置是否启用DOM存储API。默认值为 false。
要解决“disallowed_useragent”错误,一种方法是使用 Android Google Auth SDK 在应用程序中本地登录,然后通过 Google 标记到 Webview,以便 Firebase 可以将其与 auth.signInWithCredential()
.
它可能适用于其他提供商,但这是我在 Android 上使用 Google 身份验证的方式:
Android:
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);
});
}