6
votes

In my Android app I'm signing in via Firebase AuthUI using Google sign in. That part works. Now I tried to sign in using the IdToken but I always get this error message:

com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The supplied auth credential is malformed or has expired. [ Invalid id_token in IdP response: XXXXXXX... ]

This works and logs me in successfully in my Firebase project:

@OnClick(R.id.sign_in)
public void signIn() {
    startActivityForResult(
            AuthUI.getInstance().createSignInIntentBuilder()
                    .setTheme(getSelectedTheme())
                    .setLogo(getSelectedLogo())
                    .setAvailableProviders(getSelectedProviders())
                    .setTosAndPrivacyPolicyUrls(getSelectedTosUrl(),getSelectedPrivacyPolicyUrl())
                    .setIsSmartLockEnabled(true,
                            true)
                    .build(),
            RC_SIGN_IN);
}

But trying to use signInWithCredential using credentials made from the IdToken gives me the error above:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        handleSignInResponse(resultCode, data);
        return;
    }

}

@MainThread
private void handleSignInResponse(int resultCode, Intent data) {
    IdpResponse response = IdpResponse.fromResultIntent(data);

    if (resultCode == RESULT_OK) {
        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = auth.getCurrentUser();

        if(firebaseUser != null){

            auth.getCurrentUser().getIdToken(true).addOnCompleteListener(task -> {

                String idToken = task.getResult().getToken();
                AuthCredential cred = GoogleAuthProvider.getCredential(idToken ,null);

                auth.signInWithCredential(cred).addOnCompleteListener(task1 -> 
                        Log.v(TAG,"results: " + task1.getResult()));

            });
        }
    }
}

I'm following this tutorial and my goal is to sign in to multiple Firebase projects.

Trying to sign in to a secondary Firebase project using the credentials from the IdToken above returns the same error.

FirebaseApp app = FirebaseApp.getInstance("secondary");
FirebaseAuth.getInstance(app).signInWithCredential(credential);
2
check your date and time - Farido mastr

2 Answers

6
votes

I got this error when the time or time zone on the device was wrong. Setting the correct time solved it.

0
votes

I suggest you try to update the firebase auth plugin or google sign in plugin