Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Oauth NoCredentialException: No credentials available

I've been working on integrating Google OAuth into my Android app as described at

https://developer.android.com/training/sign-in/credential-manager#siwg-button

However, I encounter an error saying 'NoCredentialException: No credentials available' the moment I call credentialManager.getCredential. I have created a project in Google Cloud and added WebClient and Android Client in the OAuth settings. In my code, I have entered the ID for the WebClient. What is the solution to this frustrating error?

enter image description here

Here is My Code

    private fun googleLogin(context: Context) {
        val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
            .setFilterByAuthorizedAccounts(true)
            .setAutoSelectEnabled(true)
            .setServerClientId("!!!!Web Server Client ID!!!!!")
            .build()


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

        val credentialManager = CredentialManager.create(context)

        viewModelScope.launch {
            val loginFlow = flow {

                val result = credentialManager.getCredential(
                    context = context,
                    request = request
                )
                **// Boom! NoCredentialException: No credentials available**

                emit(result.credential)
            }.catch { e ->
                if(e is NoCredentialException) {
                    Log.e("TAG", "loginSuccess NoCredentialException", e)
                }
            }.collect {
                 loginSuccess(it)
             }
        }
    }

    private fun loginSuccess(credential: Credential) {
        when(credential) {
            is PasswordCredential -> {
                val password = credential.password
                val id = credential.id
                Log.e("TAG", "loginSuccess id: $id password: $password")
            }
            is GoogleIdTokenCredential -> {
                val idToken = credential.idToken
                Log.e("TAG", "loginSuccess idToken: $idToken")
            }
            is PublicKeyCredential -> {
                Log.e("TAG", "loginSuccess PublicKeyCredential")
            }
            is CustomCredential -> {
              
                if(credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
                    try {
                     
                        val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
                        Log.e("TAG", "loginSuccess idToken: ${googleIdTokenCredential.idToken}  ${googleIdTokenCredential.displayName}")
                    } catch (e: GoogleIdTokenParsingException) {
                        e.printStackTrace()
                    }
                }
                else {
                    Log.e("TAG", "Unexcepted CustomCredential type: ${credential.type}")
                }
            }
        }
    }

I Want Google OAuth Login In Android Native Application

like image 262
daemon Avatar asked Feb 03 '26 20:02

daemon


1 Answers

in my case,

change :

val option = GetGoogleIdOption.Builder()
    .setServerClientId(AppKey.Google.WebClientId)
    .build()

to:

val option = GetSignInWithGoogleOption.Builder(AppKey.Google.WebClientId)
    .build()

its work.

like image 142
lic leec Avatar answered Feb 06 '26 10:02

lic leec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!