gpt4 book ai didi

java - 无法向 Firebase 进行身份验证

转载 作者:行者123 更新时间:2023-12-02 10:07:32 26 4
gpt4 key购买 nike

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

最佳答案

好的,您应该首先尝试记录 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);
}

如果代码为空或不正确,请检查 firebase here 提供的示例验证您的实现是否正确

关于java - 无法向 Firebase 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55234856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com