无法通过Firebase进行身份验证

时间:2019-03-19 06:27:26

标签: java android firebase firebase-authentication

过去几天,我一直在尝试使用Firebase设置电话身份验证和Google登录。我正在获取任何OTP,并且还会收到错误“代码10”,这意味着开发人员错误,但是我已经反复检查了很多次。我已经放置了我通过Gradle-> Android-> signing报告复制的正确SHA-1密钥,放置了正确的.json文件,在firebase中启用了电话身份验证,并提供了所有依赖项。我尝试在firebase和android中制作新项目,但没有解决方案。

MainActivity.class

public class MainActivity extends AppCompatActivity {
private TextInputLayout driver_mobile;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    driver_mobile = findViewById(R.id.driver_mob);

    findViewById(R.id.send_otp).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mobile = driver_mobile.getEditText().getText().toString().trim();

            if (mobile.isEmpty() || mobile.length() < 10) {
                driver_mobile.setError("Enter a valid mobile");
                driver_mobile.requestFocus();
                return;
            }

            Intent intent = new Intent(MainActivity.this, DriverOTP.class);
            intent.putExtra("mobile", mobile);
            startActivity(intent);
        }
    });

}

DriverOTP.class

  public class DriverOTP extends AppCompatActivity {

    private EditText otp_entry;
    FirebaseAuth mAuth;
    private String OTP;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_otp);

        mAuth = FirebaseAuth.getInstance();
        otp_entry = findViewById(R.id.dmob_otp);

        Intent intent = new Intent();
        String mobile = intent.getStringExtra("mobile");
        sendOTP(mobile);

        findViewById(R.id.match_otp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = otp_entry.getText().toString().trim();
                if (code.isEmpty() || code.length() < 6) {
                    otp_entry.setError("Enter valid code");
                    otp_entry.requestFocus();
                    return;
                }

                verifyOTP(code);
            }
        });
    }

    private void sendOTP(String mobile){
        PhoneAuthProvider.getInstance().verifyPhoneNumber("+91" + mobile,
                60,
                TimeUnit.SECONDS,
                TaskExecutors.MAIN_THREAD,
                mCallbacks);
    }

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

            String code = phoneAuthCredential.getSmsCode();

            if(code!=null){
                otp_entry.setText(code);
                verifyOTP(code);
            }

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            Toast.makeText(DriverOTP.this, e.getMessage(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken){
            super.onCodeSent(s, forceResendingToken);

            OTP = s;
        }
    };
    private void verifyOTP(String code) {
        //creating the credential
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(OTP, code);

        //signing the user
        signInWithPhoneAuthCredential(credential);
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(DriverOTP.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            //verification successful we will start the profile activity
                            Intent intent = new Intent(DriverOTP.this, Driver_Form.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);

                        } else {

                            //verification unsuccessful.. display an error message

                            String message = "Somthing is wrong, we will fix it soon...";

                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                message = "Invalid code entered...";
                            }

                            Snackbar snackbar = Snackbar.make(findViewById(R.id.parent), message, Snackbar.LENGTH_LONG);
                            snackbar.setAction("Dismiss", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            });
                            snackbar.show();
                        }
                    }
                });
    }
}

依赖项

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:animated-vector-drawable:28.0.0'
    implementation 'com.android.support:support-media-compat:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.2.0'
    implementation 'com.google.firebase:firebase-core:16.0.7'
    implementation 'com.google.firebase:firebase-database:16.1.0'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

apply plugin: 'com.google.gms.google-services'

错误

2019-03-19 12:45:38.119 15180-15180/microsquare.goicabs.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: microsquare.goicabs.test, PID: 15180
java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, ortemprary proof.
    at com.google.android.gms.common.internal.Preconditions.checkArgument(Unknown Source:35)
    at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source:6)
    at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source:33)
    at microsquare.goicabs.test.DriverOTP.verifyOTP(DriverOTP.java:94)
    at microsquare.goicabs.test.DriverOTP.access$100(DriverOTP.java:24)
    at microsquare.goicabs.test.DriverOTP$1.onClick(DriverOTP.java:53)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25890)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6739)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:859)

1 个答案:

答案 0 :(得分:1)

好的,因此您应该首先尝试记录OPT代码以验证其不为null或不正确。

    private void verifyOTP(String code) {
    //creating the credential
    Log.d(TAG, "VerifyOPT Code: " + OPT)
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(OTP, code);
    //signing the user
    signInWithPhoneAuthCredential(credential);
}

如果代码为null或不正确,请检查firebase here提供的示例,以验证您的实现正确无误