CredentialManager with GoogleId always "cancelled"
Asked Answered
O

2

8

Whichever way I implement compose credential manager, I get an exception GetCredentialCancellationException: activity is canceled by the user.

The manager shows a dialog with my Google accounts, I choose one and then there is a "consent screen" with a "continue as me" button. After clicking that, I get the exception above.

I don't know what I am doing wrong.

My libraries:
androidx.credentials.credentials:credentials:1.2.0-beta03
androidx.credentials.credentials:credentials-play-services-auth:1.2.0-beta03
com.google.android.gms:play-services-auth:20.7.0

The basic demo code:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            CredentialManager.create([email protected]).getCredential(
                context = this@MainActivity,
                request = GetCredentialRequest(
                    listOf(
                        GetGoogleIdOption.Builder()
                            .setFilterByAuthorizedAccounts(false)
                            .setAutoSelectEnabled(false)
                            .setServerClientId("secret-id")
                            .build(),
                    )
                ),
            ).credential
        }

        setContent {
            Text("DEMO")
        }
    }
}
Oireachtas answered 2/9, 2023 at 14:5 Comment(0)
O
13

Okay, finally figured it out after x days. The exception is wrong, it has nothing to do with the problem.

I had wrong client id. It is not supposed to be Android client id, but WebApplication client id.

I found it out after checking if google sign in via firebase would work and it was clearly written there, in step 1, point b. https://firebase.google.com/docs/auth/android/google-signin#authenticate_with_firebase

Oireachtas answered 2/9, 2023 at 18:59 Comment(3)
Thank you!! Just to add, the rest of the documentation for android sign in appears to be correct, specifically you don't need the client secret you get from the cloud console, just client id.Shellyshelman
You saved the day! The documentation is really bad...Schlock
Finally worked. The name was Web client (auto created by Google Service).Hildegaard
F
0

Just found the solution for me, I added the following in my dependencies:

implementation "androidx.credentials:credentials:1.2.2"
implementation "androidx.credentials:credentials-play-services-auth:1.2.2"
implementation "com.google.android.libraries.identity.googleid:googleid:1.1.0"

and it works!

Furcula answered 13/4 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.