验证失败(Firebase安卓)

huangapple go评论109阅读模式
英文:

Verification failed(Firebase android)

问题

这是关于使用 Firebase 进行电话号码验证的应用程序。但我一直收到“验证失败”的消息。以下是 OTP 验证的代码:

  1. private void sendVerificationCode(){
  2. String phoneN =phone.getText().toString();
  3. PhoneAuthProvider.getInstance().verifyPhoneNumber(
  4. phoneN,
  5. 60,
  6. TimeUnit.SECONDS,
  7. Registration.this,
  8. mCallbacks);
  9. }
  10. PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks= new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
  11. @Override
  12. public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
  13. Toast.makeText(Registration.this,"验证完成",Toast.LENGTH_SHORT).show();
  14. signInWithPhoneAuthCredential(phoneAuthCredential);
  15. }
  16. @Override
  17. public void onVerificationFailed(FirebaseException e) {
  18. Toast.makeText(Registration.this,"验证失败",Toast.LENGTH_SHORT).show();
  19. }
  20. @Override
  21. public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
  22. codeSent=s;
  23. }
  24. };
  25. private void VerifyCode(){
  26. String code= otp.getText().toString();
  27. PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
  28. signInWithPhoneAuthCredential(credential);
  29. }
  30. private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
  31. mAuth.signInWithCredential(credential)
  32. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  33. @Override
  34. public void onComplete(@NonNull Task<AuthResult> task) {
  35. if (task.isSuccessful()) {
  36. Toast.makeText(getApplicationContext(),"欢迎,现在您可以登录", Toast.LENGTH_LONG).show();
  37. Intent intent = new Intent(Registration.this,Login_Reg.class);
  38. startActivity(intent);
  39. } else {
  40. if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
  41. // 输入的验证码无效
  42. Toast.makeText(getApplicationContext(),"验证码不正确",Toast.LENGTH_LONG).show();
  43. }
  44. }
  45. }
  46. });
  47. }
  48. }

我一直收到“验证失败”的消息。以下是用户界面(UI)。

验证失败(Firebase安卓)

如何解决这个问题?

英文:

I am trying to make an app for phone number authentication through firebase. But I keep getting the message verification failed.
Here is the code for OTP authentication:-

  1. private void sendVerificationCode(){
  2. String phoneN =phone.getText().toString();
  3. PhoneAuthProvider.getInstance().verifyPhoneNumber(
  4. phoneN, 60,
  5. TimeUnit.SECONDS,
  6. Registration.this,
  7. mCallbacks);
  8. }
  9. PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks= new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
  10. @Override
  11. public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
  12. Toast.makeText(Registration.this,&quot;verification completed&quot;,Toast.LENGTH_SHORT).show();
  13. signInWithPhoneAuthCredential(phoneAuthCredential);
  14. }
  15. @Override
  16. public void onVerificationFailed(FirebaseException e) {
  17. Toast.makeText(Registration.this,&quot;verification failed&quot;,Toast.LENGTH_SHORT).show();
  18. }
  19. @Override
  20. public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
  21. //super.onCodeSent(s, forceResendingToken);
  22. codeSent=s;
  23. }
  24. };
  25. private void VerifyCode(){
  26. String code= otp.getText().toString();
  27. PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
  28. signInWithPhoneAuthCredential(credential);
  29. }
  30. private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
  31. mAuth.signInWithCredential(credential)
  32. .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
  33. @Override
  34. public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  35. if (task.isSuccessful()) {
  36. Toast.makeText(getApplicationContext(),&quot;Welcome, You can login now&quot;, Toast.LENGTH_LONG).show();
  37. Intent intent = new Intent(Registration.this,Login_Reg.class);
  38. startActivity(intent);
  39. } else {
  40. if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
  41. // The verification code entered was invalid
  42. Toast.makeText(getApplicationContext(),&quot;Incorrect Verification Code&quot;,Toast.LENGTH_LONG).show();
  43. }
  44. }
  45. }
  46. });
  47. }
  48. }

I keep getting the message "verification failed".
Below is the UI.

验证失败(Firebase安卓)

How to solve this?

答案1

得分: 0

已经找到解决方案...尝试在实际设备上运行。它会起作用的。

英文:

Got the solution...Try to run it on an actual device. It will work.

huangapple
  • 本文由 发表于 2020年10月16日 14:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定