问题
private void verifyphone(final String phone)
{
final String ph=phone;
pDialog=new Dialog(this);
pDialog.setContentView(R.layout.phoneverification);
pDialog.setCancelable(true);
pcode=pDialog.findViewById(R.id.pcodebtn);
vcode=pDialog.findViewById(R.id.vcode);
pstatus=pDialog.findViewById(R.id.pstatus);
pDialog.show();
PhoneAuthProvider.getInstance().verifyPhoneNumber(phone, 30, TimeUnit.SECONDS, this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(HomePage.this, "Verfied Phone", Toast.LENGTH_SHORT).show();
signincredentials(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
// ...
Toast.makeText(HomePage.this, "Invalid Phone Number", Toast.LENGTH_SHORT).show();
pDialog.dismiss();
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
// ...
Toast.makeText(HomePage.this, "SMS Quota exceeded.This is an internal error.", Toast.LENGTH_SHORT).show();
}
Toast.makeText(HomePage.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
Toast.makeText(HomePage.this, "Verification Code sent to the phone number", Toast.LENGTH_SHORT).show();
pstatus.setText("Verification Code has been sent to "+ph+".Please check phone and enter code to continue.");
pvercode=s;
mtoken=forceResendingToken;
}
});
pcode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(vcode.getText().toString())) {
vcode.setError("Verification Code cannot empty");
} else if (vcode.getText().toString().length() != 6) {
vcode.setError("Verification code format is wrong");
}
else
{
pauthcreddential=PhoneAuthProvider.getCredential(pvercode,vcode.getText().toString());
signincredentials(pauthcreddential);
}
}
});
}
public void signincredentials(PhoneAuthCredential pcredential)
{
mauth.signInWithCredential(pcredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
pverified=true;
user = task.getResult().getUser();
Toast.makeText(HomePage.this, "Phone number verification sucessful", Toast.LENGTH_SHORT).show();
verifyemail();
} else {
if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
pstatus.setText("Verification was unsuccessful.You entered wrong code!");
vcode.setError("Wrong Verification code was entered.");
Toast.makeText(HomePage.this, "Pverified="+vcode.getText(), Toast.LENGTH_SHORT).show();
}
}
}
});
}
public void verifyemail()
{
pDialog.dismiss();
mauth.signOut();
mauth=FirebaseAuth.getInstance();
Toast.makeText(this, "Processing email verification", Toast.LENGTH_SHORT).show();
mauth.createUserWithEmailAndPassword(rmail.getText().toString(), rpass.getText().toString()).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
user = mauth.getCurrentUser();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(HomePage.this, "We have sent you an email verification.", Toast.LENGTH_SHORT).show();
}
}
});
Toast.makeText(HomePage.this, "Registration sucessful", Toast.LENGTH_SHORT).show();
myDialog.dismiss();
}
else
{
rmail.setError("Enter a correct mailID we will be sending a verification mail.");
Toast.makeText(HomePage.this, "Wrong EmailID"+rmail.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
I am always getting wrong verification code as output even when I enter the correct OTP(even when I enter valid OTP for whitelist numbers). I have been trying for hours to fix this. Please help me out.
I am not facing this issue during automatic verification of OTP. Instead, the latter part of the code which is to verify mail fails
The error seems to be related to signincredentials() method. When I ran in the emulator, I am getting the following error: LOGCAT details
08-01 20:21:17.634 16978-16978/akshay.shoppingapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: akshay.shoppingapplication, PID: 16978 java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, ortemprary proof. at com.google.android.gms.common.internal.zzbq.checkArgument(Unknown Source:8) at com.google.firebase.auth.PhoneAuthCredential.(Unknown Source:40) at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source:9) at akshay.shoppingapplication.HomePage$4.onClick(HomePage.java:188) 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:25883) 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:6642) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 08-01 20:21:17.686 16978-16978/akshay.shoppingapplication I/Process: Sending signal. PID: 16978 SIG: 9
来源:https://stackoverflow.com/questions/51636733/phone-authentication-always-producing-negative-responses-in-firebase