Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase phone authentication INVALID_APP_CREDENTIAL:App validation failed

I am really new to android firebase and I have implemented the necessary libraries for Firebase Auth. I try putting a valid number, but the log says its:

W/JEJE: onVerificationFailed
com.google.firebase.FirebaseException: An internal error has occurred. [ INVALID_APP_CREDENTIAL:App validation failed ]
at com.google.android.gms.internal.nf.zzK(Unknown Source)
at com.google.android.gms.internal.og.zza(Unknown Source)
at com.google.android.gms.internal.oh.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6176)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

Here is my code:

public class MainActivity extends AppCompatActivity {

    private static String mVerificationId;
    private static PhoneAuthProvider.ForceResendingToken mResendToken;
    private static FirebaseAuth mAuth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText = (EditText) findViewById(R.id.phone);
        Button submit = (Button) findViewById(R.id.submit);

        final PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                Log.d("JEJE", "onVerificationCompleted:" + phoneAuthCredential);

                signInWithPhoneAuthCredential(phoneAuthCredential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                Log.w("JEJE", "onVerificationFailed", e);

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    Log.d("JEJE", "INVALID REQUEST");
                } else if (e instanceof FirebaseTooManyRequestsException) {
                    Log.d("JEJE", "Too many Request");
                }
            }

            @Override
            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                Log.d("JEJE", "onCodeSent:" + s);

                mVerificationId = s;
                mResendToken = forceResendingToken;

            }
        };

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNum = editText.getText().toString();
                Toast.makeText(MainActivity.this, phoneNum, Toast.LENGTH_SHORT).show();
                verifyPhone(phoneNum,mCallBacks);
            }

        });
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) {
        mAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    FirebaseUser user = task.getResult().getUser();
                }else {
                    if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                        // The verification code entered was invalid
                    }
                }
            }
        });
    }

    public void verifyPhone(String phoneNumber, PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks){
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                "+639952874699",        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallback
    }

}

please tell me whats wrong thanks..

like image 474
Coozgan Avatar asked Jun 08 '17 09:06

Coozgan


2 Answers

Adding SHA Certificate Fingerprint solves my problem. Well, Im fairly new to this but I managed to show output and I was able to received a sms verification.

For reference here are my codes: https://github.com/coozgan/TestingPhoneAuth

like image 146
Coozgan Avatar answered Sep 21 '22 11:09

Coozgan


Well after a while I'll just summarize all I found out about the phone verification. There are a few things you need to do and should remember for it to work.

These are the list of things you need to do in order for it to work:

  1. Create an app and connect it to yours with the google-services.json you got from firebase. Put it in the correct folder and make sure you added in the Gradle compile 'com.google.firebase:firebase-auth:11.0.4'.
  2. In your app on firebase, go to Authentication on the menu. Then go to Sign-in Method and make sure to enable the phone provider.
  3. Add a Sha-1 to your app. When going to project settings inside your project, go to the bottom of your app section and youll see a place to put the Sha-1. In order to get the Sha-1 you need to run this command

keytool -exportcert -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore

as mentioned in the firebase guide for it. Notice this command is on line so run it all together, and what it will do is it will look for your debug.keystore file on your user in a hidden folder called .android. The default alias for the keystore is androiddebugkey so dont change it and the default password for it is android, So when it asks you for a password just use "android". Then youll see a list of returns and one of them is Sha-1. Put it in your project.

  1. If still nothing works, this is not always needed but make sure that your app is linked to a google account. In your Project settings->Account Linking->Google Play.

Also notice, this is written of the 16th of august 2017, you cannot do the phone verification on an emulator, so use a real device.

Hope it helps.

Thanks saurabh Yadav for the missing slash ;) apperently I have to put 2 slashes here in order for it to show.

like image 35
Shai Avatar answered Sep 18 '22 11:09

Shai