未在 Firebase 电话身份验证中接收代码。

use*_*839 6 android firebase firebase-authentication

我正在尝试在我的应用中实施 Firebase 电话身份验证。我参考了 GitHub 上的 firebase android 文档,但无法通过短信获取代码。

我不知道为什么会这样?我正在真实设备上对其进行测试。

另外,我正在向另一个人发送短信,因为真实设备没有 SIM 卡。但我确信这不是问题。这样对吗?

这是我的代码

 void logIn(){

    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("verification", "onVerificationCompleted:" + credential);
            // [START_EXCLUDE silent]
            mVerificationInProgress = false;
            // [END_EXCLUDE]

            // [START_EXCLUDE silent]
            // Update the UI and attempt sign in with the phone credential
            // [END_EXCLUDE]
            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("verification", "onVerificationFailed", e);
            // [START_EXCLUDE silent]
            mVerificationInProgress = false;
            // [END_EXCLUDE]

            if (e instanceof FirebaseAuthInvalidCredentialsException) {

            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // [START_EXCLUDE]
                Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                        Snackbar.LENGTH_SHORT).show();
                // [END_EXCLUDE]
            }


        }

        @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("verification", "onCodeSent:" + verificationId);

            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;


        }
    };

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            "+79995198722",        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks

    mVerificationInProgress = true;
}

private void verifyPhoneNumberWithCode(String verificationId, String code) {
    // [START verify_with_code]
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    // [END verify_with_code]
    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("signIn", "signInWithCredential:success");

                        FirebaseUser user = task.getResult().getUser();

                    } else {
                        // Sign in failed, display a message and update the UI
                        Log.w("signIn", "signInWithCredential:failure", task.getException());
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {

                        }

                    }
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Shy*_*dda 6

我认为您的问题可能是SHA-1 指纹,因此您需要更新 Firebase 控制台中的 SHA-1 密钥。

脚步:

登录Firebase 控制台后

  1. 单击您的项目,然后您可以看到您的项目概述
  2. 单击三个点,然后单击设置
  3. 单击添加指纹(底部)
  4. 粘贴您的 SHA-1 密钥,单击“保存”。

SHA-1 密钥的步骤:

  1. 从左侧面板中选择 Android Studio 中的 gradle
  2. 选择您的应用
  3. 转到任务 -> android -> 签名报告

然后你可以看到Sha-1键。

在此处输入图片说明