【问题标题】:Not able to authenticate to Firebase无法向 Firebase 进行身份验证
【发布时间】:2019-08-09 14:49:33
【问题描述】:

在过去的几天里,我一直在尝试使用 firebase 设置电话身份验证和谷歌登录。我收到任何 OTP 并且还收到错误“代码 10”,这意味着开发人员错误,但我已经交叉检查了很多次。我已经放置了通过 Gradle->Android->签名报告复制的正确 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)

【问题讨论】:

  • 你最近的错误是什么,请把日志记录下来。没有日志,我们真的不知道发生了什么
  • 我无法收到任何短信,所以我在 firebase 中输入了手机号码和代码进行测试。
  • 您是否包含已启用手机身份验证的最新 google-services.json?
  • @AngusTay 是的,我做到了
  • 有什么更新吗?如果您发现@RumitPatel 或其他人建议的答案是正确的,请指出它或者您有其他解决方案,请随时自己创建一个答案;)

标签: java android firebase firebase-authentication


【解决方案1】:

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

    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 提供的示例以验证您的实现是否正确

【讨论】:

  • 我会试试的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
相关资源
最近更新 更多