从 Playstore 下载应用程序时 Google 身份验证失败,但从 Android studio 安装应用程序时 Google 身份验证有效
我通过 Firebase 进行 Google 身份验证时遇到了一些奇怪的问题。
当我从 google play 商店安装它时,谷歌身份验证无法工作,但另一方面,当我从 Android studio(相同版本)本地安装它时,它确实可以工作。
以下是身份验证功能 -
private fun signInWithGoogle() {
// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
val signInIntent = googleSignInClient.signInIntent
getResult.launch(signInIntent)
}
private val getResult =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
if (task.isSuccessful) {
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)!!
firebaseAuthWithGoogle(account.idToken!!)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
}
} else {
println(task.exception?.message)
}
}
}
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
mAuth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
println("signInWithCredential:success")
startActivity(Intent(activity, MainActivityApplication::class.java))
activity?.finish()
} else {
// If sign in fails, display a message to the user.
println("signInWithCredential:failure ${task.exception}")
}
}
.addOnFailureListener {
println(it.message)
binding.pbGoogleBtn.visibility= View.GONE
}
}
I ran into some strange issue with Google-Authentication through Firebase.
When I install it from google play store, google-authentication fails to work, but in the other hand when I install it locally from Android studio (same version), it does work.
the following is the authentication function -
private fun signInWithGoogle() {
// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
val signInIntent = googleSignInClient.signInIntent
getResult.launch(signInIntent)
}
private val getResult =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
if (task.isSuccessful) {
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)!!
firebaseAuthWithGoogle(account.idToken!!)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
}
} else {
println(task.exception?.message)
}
}
}
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
mAuth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
println("signInWithCredential:success")
startActivity(Intent(activity, MainActivityApplication::class.java))
activity?.finish()
} else {
// If sign in fails, display a message to the user.
println("signInWithCredential:failure ${task.exception}")
}
}
.addOnFailureListener {
println(it.message)
binding.pbGoogleBtn.visibility= View.GONE
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论