为什么Firebase在Google Play验证成功后返回“null”帐户?
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[...]
val activity = this
val gso = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
.requestServerAuthCode("<some-id>.apps.googleusercontent.com")
.build()
val signInClient = GoogleSignIn.getClient(activity, gso)
val acc = GoogleSignIn.getLastSignedInAccount(activity) // This is null
val currentUser = Firebase.auth.currentUser // This is null
signInClient.silentSignIn().addOnCompleteListener(activity) { task ->
if (task.isSuccessful) {
val account = task.result // This is com.google.android.gms.auth.api.signin.GoogleSignInAccount@123456
account.displayName // This is null
account.id // This is null
Firebase.auth.currentUser // Still null
}
}
}
}
字符串
根据文档,如果“isSuccessful”被触发,则用户ID不应为“null”:https://firebase.google.com/docs/auth/android/play-games
1条答案
按热度按时间ffvjumwh1#
事实上,您只能使用Google进行身份验证,但如果您还想使用Firebase产品,那么您也应该在Firebase中进行身份验证。这意味着您应该遵循以下步骤。首先,使用Google登录。一旦身份验证成功,获取Google凭据并将其传递给FirebaseAuth#signInWithCredential(com.google.firebase.auth.AuthCredential)方法。请查看下面的One Tap sign-in and sign-up文档。这里是实现Firebase sign-in with Google的文档。
这里也有一个resource可能会有帮助,对应的repo。
虽然上面的解决方案肯定会工作,但请注意,One Tap登录很快就会被弃用。所以你应该考虑Integrate Credential Manager with Sign in with Google。