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
1 Answer
Reset to default 0You'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!