Android Firebase phone authentication INVALID_APP_CREDENTIAL:App validation failed

心已入冬 提交于 2019-12-10 02:23:08

问题


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..


回答1:


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




回答2:


I had the exact same issue as yours. Even though I uploaded both the SHA1 it would still give a "Phone login error" prompt.

You need to link the Firebase project with Google Play.

Head to https://console.firebase.google.com/u/0/project/you-project-name/settings/accountlinking and on the bottom right click "Link to Google Play"




回答3:


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.




回答4:


I have also same problem.

After read some content i found some other problem and after run my application.

Problem 1: this link says that,

Also, note that phone number sign-in requires a physical device and won't work on an emulator.

So can you test and run only on real device not any emulator.

If you can run on emulator then same error gives.

You can refer my github this link for Firebase Phone Login Example




回答5:


Try upgrading your firebase-tools lib to the latest version, then call firebase logout followed by firebase login.




回答6:


If you are going for signed build and put it in play store,the sha1 of play store should be same as the sha1 of Firebase fingerprint. And once it's done you need to link your app with the play store through firebase login.Replace the new google-services.json file in your android app from the new one generated in firebase account after the sha1 key change.



来源:https://stackoverflow.com/questions/44432204/android-firebase-phone-authentication-invalid-app-credentialapp-validation-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!