首页 > 解决方案 > 获取 Google 签名的用户信息

问题描述

我有一个应用程序可以获取 Google 凭据,以便将用户注册到我的 API。我需要idToken谷歌流程提供的。

按照教程,我能够登录。

然后,我有一个启动画面。我希望它验证用户是否已被 Google 允许进入主要活动。如果没有,请转到登录活动。

但是根据教程的代码,它使用下面的代码询问 Firebase,它当然为我提供了一个 Firebase 令牌,而不是 Google 令牌。

override fun onStart() {
    super.onStart()
    val user = FirebaseAuth.getInstance().currentUser
    if (user != null) {
        startActivity(MainActivity.getLaunchIntent(this))
        finish()
    }
}

所以,问题是:如何获取谷歌信息来检索令牌,以便我的 API 可以验证令牌是否?

标签: androidkotlingoogle-signin

解决方案


If someone else need to implements something alike, I'll share how I did it. There is a silentSignIn from Google SDK which allows to do it.

So, this is the code:

val signInOptions = getSignInOptions(this)
val googleSignInClient = GoogleSignIn.getClient(this, signInOptions)
googleSignInClient.silentSignIn().addOnCompleteListener {
    val account: GoogleSignInAccount? = it.result
    if (account != null) {
        // success
    }
    else {
        // failure
    }
}

推荐阅读