【发布时间】:2018-02-07 05:35:48
【问题描述】:
我正在尝试验证电话号码,但始终失败。我有需要手机号码的登录活动。单击登录按钮后,我希望 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
【问题讨论】:
-
你能告诉我们错误日志吗??
-
没有错误。
-
检查编辑后的答案
-
它正在进行 onVerificationFailed 意味着它正在调用firebase,但我猜有些东西丢失了。我不知道它是什么。
-
哦验证 SHA1 密钥和生成的 json 文件是否有效。如果不是其中任何一个,这可能是唯一的原因
标签: android firebase firebase-authentication