最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Android: How to check if the user is logged in to the Play Store - Stack Overflow

programmeradmin1浏览0评论

Using Kotlin, I want to verify that the user is logged in to the PlayStore before sending it to update the app.

I'm using credential manager because the other options I could find are deprecated.

Credential manager works fine when I use it to log into my app, but it doesn't give me information about whether user is logged into the PlayStore.

NoCredentialException is always thrown if I don't add the credentials inside my app.

private suspend fun checkGoogleAccount(context: Context) {
    val credentialManager = CredentialManager.create(context)
    try {

        val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
            .setFilterByAuthorizedAccounts(false)
            .setAutoSelectEnabled(false)
            .setServerClientId(getString(R.string.web_client_id))
            .build()

        val request: GetCredentialRequest = GetCredentialRequest.Builder()
            .addCredentialOption(googleIdOption)
            .build()

        val credentialResponse = credentialManager.getCredential(
            context = context,
            request = request
        )
        android.util.Log.e("TAG","credentialResponse ${credentialResponse.credential}")
    } catch (e: GetCredentialCancellationException) {
        android.util.Log.e("TAG", "GetCredentialCancellationException ${e.message}")
    } catch (e: NoCredentialException) {
        android.util.Log.e("TAG", "NoCredentialException")
    } catch (e: GetCredentialException) {
        android.util.Log.e("TAG", "GetCredentialException $e")
    } catch (e: Exception) {
        android.util.Log.e("TAG", "Exception ${e.message}")
    }
}

I also tried using ReviewManagerFactory as suggested in this post, but it always tells me that the user is logged in, when he is not:

private fun checkReviewManager(activity: Activity) {
    val reviewManager = ReviewManagerFactory.create(activity)
    val request = reviewManager.requestReviewFlow()
    request.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val reviewInfo = task.result
            val flow = reviewManager.launchReviewFlow(context as Activity, reviewInfo)
            flow.addOnCompleteListener { launchTask ->
                if (launchTask.isSuccessful) {
                    android.util.Log.e("TAG", "User is logged, goto playStore")
                } else {
                    android.util.Log.e("TAG", "User in not logged, goto googleLogin")
                }
            }
        } else {
            android.util.Log.e("TAG", "task Fail: ${task.exception}")
        }
    }
}

Using Kotlin, I want to verify that the user is logged in to the PlayStore before sending it to update the app.

I'm using credential manager because the other options I could find are deprecated.

Credential manager works fine when I use it to log into my app, but it doesn't give me information about whether user is logged into the PlayStore.

NoCredentialException is always thrown if I don't add the credentials inside my app.

private suspend fun checkGoogleAccount(context: Context) {
    val credentialManager = CredentialManager.create(context)
    try {

        val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
            .setFilterByAuthorizedAccounts(false)
            .setAutoSelectEnabled(false)
            .setServerClientId(getString(R.string.web_client_id))
            .build()

        val request: GetCredentialRequest = GetCredentialRequest.Builder()
            .addCredentialOption(googleIdOption)
            .build()

        val credentialResponse = credentialManager.getCredential(
            context = context,
            request = request
        )
        android.util.Log.e("TAG","credentialResponse ${credentialResponse.credential}")
    } catch (e: GetCredentialCancellationException) {
        android.util.Log.e("TAG", "GetCredentialCancellationException ${e.message}")
    } catch (e: NoCredentialException) {
        android.util.Log.e("TAG", "NoCredentialException")
    } catch (e: GetCredentialException) {
        android.util.Log.e("TAG", "GetCredentialException $e")
    } catch (e: Exception) {
        android.util.Log.e("TAG", "Exception ${e.message}")
    }
}

I also tried using ReviewManagerFactory as suggested in this post, but it always tells me that the user is logged in, when he is not:

private fun checkReviewManager(activity: Activity) {
    val reviewManager = ReviewManagerFactory.create(activity)
    val request = reviewManager.requestReviewFlow()
    request.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val reviewInfo = task.result
            val flow = reviewManager.launchReviewFlow(context as Activity, reviewInfo)
            flow.addOnCompleteListener { launchTask ->
                if (launchTask.isSuccessful) {
                    android.util.Log.e("TAG", "User is logged, goto playStore")
                } else {
                    android.util.Log.e("TAG", "User in not logged, goto googleLogin")
                }
            }
        } else {
            android.util.Log.e("TAG", "task Fail: ${task.exception}")
        }
    }
}
Share Improve this question edited Jan 30 at 20:16 CPlus 4,93045 gold badges30 silver badges73 bronze badges asked Jan 30 at 15:50 Alejandro GiraudoAlejandro Giraudo 11 bronze badge 2
  • Checking if a user is logged in Play Store is not possible for third party applications due to security restrictions in Android OS. – Livio Commented Jan 30 at 15:57
  • The app is for ATV and if I send it to update when the user is not logged, the user goes to a screen that they cannot exit. Is there a work around? – Alejandro Giraudo Commented Jan 30 at 19:05
Add a comment  | 

1 Answer 1

Reset to default 0

You're getting NoCeredentialException because Credential Manager doesn't support the functionality that your code intents to find out. Instead try using Google Play In-App Review API. It works only if the user is already logged in, will fail if not.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论