首页 > 解决方案 > 验证失败(Firebase 安卓)

问题描述

我正在尝试通过firebase制作一个用于电话号码身份验证的应用程序。但是我一直收到消息验证失败。这是 OTP 身份验证的代码:-

    private void sendVerificationCode(){
        String phoneN =phone.getText().toString();
        
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneN,                           60,                 
                TimeUnit.SECONDS,   
                Registration.this,               
                mCallbacks);        

    }

    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks= new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
            Toast.makeText(Registration.this,"verification completed",Toast.LENGTH_SHORT).show();
            signInWithPhoneAuthCredential(phoneAuthCredential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            Toast.makeText(Registration.this,"verification failed",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            //super.onCodeSent(s, forceResendingToken);
            codeSent=s;
        }
    };
    private void VerifyCode(){
        String code= otp.getText().toString();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
        signInWithPhoneAuthCredential(credential);
    }
    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(getApplicationContext(),"Welcome, You can login now", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(Registration.this,Login_Reg.class);
                            startActivity(intent);

                        } else {
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                                Toast.makeText(getApplicationContext(),"Incorrect Verification Code",Toast.LENGTH_LONG).show();
                            }

                        }
                    }
                });
    }
}

我不断收到“验证失败”的消息。下面是用户界面。

在此处输入图像描述

如何解决这个问题?

标签: javaandroidfirebasefirebase-authentication

解决方案


得到解决方案...尝试在实际设备上运行它。它会起作用的。


推荐阅读