我正在尝试验证电话号码,但它一直都失败了。我有登录活动,需要手机号码。点击登录按钮后我想打开OTP活动,用户可以在这里输入OTP,点击验证按钮验证号码。但它始终在onVerificationFailed块上显示,并显示Toast Verification Fail和Invalid Number。
登录活动
public class LoginMain extends AppCompatActivity {
public FirebaseAuth mAuth;
// [END declare_auth]
public String mVerificationId;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
public PhoneAuthProvider.ForceResendingToken mResendToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_main);
final EditText txtMobileno = (EditText) findViewById(R.id.txtMobileno);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
mAuth = FirebaseAuth.getInstance();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(LoginMain.this, "Verification Done" + phoneAuthCredential, Toast.LENGTH_LONG).show();
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(LoginMain.this, "Verification Fail", Toast.LENGTH_LONG).show();
if (e instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(LoginMain.this, "Invalid Number", Toast.LENGTH_SHORT).show();
} else if (e instanceof FirebaseTooManyRequestsException) {
Toast.makeText(LoginMain.this, "Too many Request", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
mVerificationId = s;
mResendToken = forceResendingToken;
Toast.makeText(LoginMain.this, "Code Sent", Toast.LENGTH_SHORT).show();
}
};
txtMobileno.setText("+"+getCountrycode());
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mobileno = txtMobileno.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber( mobileno, 60, TimeUnit.SECONDS, LoginMain.this, mCallbacks);
}
});
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's informatio
Toast.makeText(LoginMain.this, "Verification done", Toast.LENGTH_LONG).show();
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(LoginMain.this, "Verification failed code invalid", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
OTP活动
public class OTP extends AppCompatActivity {
public FirebaseAuth mAuth;
public String mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
mAuth = FirebaseAuth.getInstance();
Button btnVerify = (Button) findViewById(R.id.btnVerifyOTP);
final EditText txtOtp = (EditText) findViewById(R.id.txtOtp);
btnVerify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId , txtOtp.getText().toString());
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()) {
// Sign in success, update UI with the signed-in user's information
//Log.d(TAG, "signInWithCredential:success");
Toast.makeText(OTP.this,"Verification done",Toast.LENGTH_LONG).show();
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
//Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(OTP.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
错误:
[FirebaseAuth:] getGoogleApiForMethod()返回Gms
答案 0 :(得分:0)
当您发送的电话号码格式无效或者您可能错过了正确的google_service.json文件时,通常会发生验证失败或无效号码。
一种简单格式包括使用正确的国家/地区代码发送电话号码。请添加代码并发送。希望这有效。