Android Firebase电话身份验证INVALID_APP_CREDENTIAL

时间:2017-08-26 08:22:38

标签: android firebase firebase-authentication

我已按照所有步骤为firebase配置phone auth但仍然无法从firebase获取OTP。

是否必须将Play商店帐户链接到firebase for phone auth。

请帮忙!!!!!

1 个答案:

答案 0 :(得分:0)

  1. 在firebase身份验证登录方法中启用手机身份验证。

  2. 在onCreate方法中过去了这段代码。

       mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    
        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verificaiton without
            //     user action.
            Log.d(TAG, "onVerificationCompleted:" + credential);
    
            //signInWithPhoneAuthCredential(credential);
        }
    
        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            Log.w(TAG, "onVerificationFailed", e);
    
            Toast.makeText(SignUp.this, e.getMessage(), Toast.LENGTH_LONG).show();
    
            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // ...
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // ...
            }
    
            // Show a message and update the UI
            // ...
        }
    
        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            Log.d(TAG, "onCodeSent:" + verificationId);
    
            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;
    
            // ...
        }
    };
    
  3. 我关闭了自动验证功能。用户总是必须输入代码。

    1. 将代码发送给用户

      String phoneNumber = phoneNumberET.getText().toString();
      PhoneAuthProvider.getInstance().verifyPhoneNumber(
              phoneNumber,        // Phone number to verify
              60,                 // Timeout duration
              TimeUnit.SECONDS,   // Unit of timeout
              this,               // Activity (for callback binding)
              mCallbacks);        // OnVerificationStateChangedCallbacks
      
    2. 从用户处获取代码并发送以验证

      String code = mCodeEt.getText().toString();
      PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);
      signInWithPhoneAuthCredential(credential);
      
    3. 此方法为结果方法

      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");
      
                          FirebaseUser user = task.getResult().getUser();
                          //Sucess do what u want to do
                          // [START_EXCLUDE]
                          //updateUI(STATE_SIGNIN_SUCCESS, user);
                          // [END_EXCLUDE]
                      } 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
                              // [START_EXCLUDE silent]
                              mCodeEt.setError("Invalid code.");
                              // [END_EXCLUDE]
                          }
                          // [START_EXCLUDE silent]
                          // Update UI
                          //updateUI(STATE_SIGNIN_FAILED);
                          // [END_EXCLUDE]
                      }
                  }
              });
             }
      
    4. 所有代码均来自firebase doc ....