当前位置: 首页 > 知识库问答 >
问题:

使用android studio进行firebase身份验证时出现问题,代码未发送

颜瀚漠
2023-03-14

我已经将SHA1和SHA-256包括在firebase项目中。

当片段运行异常吐司消息时,会出现:“此请求缺少一个有效的应用索引器,这意味着SafetyNet检查和reCAPTCHA检查都没有成功。请重试,或检查logcat了解更多详细信息”。

代码:

public class VerifyPhoneFragment extends Fragment {

    public static String source = null;
    PinView pinView;
    String phoneNo;
    boolean verified = false;
    private String VerificationCodSent;
    private String CodeEnteredByUser;


    public VerifyPhoneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){

        final NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);
        final ProgressBar progressBar = view.findViewById(R.id.progress_bar);
        ImageView backArrowImageView = view.findViewById(R.id.back_arrow);
        TextView backTextView = view.findViewById(R.id.back_text_view);
        TextView otpMobileTextView = view.findViewById(R.id.otp_mobile_tv);
        Button verifyButton = view.findViewById(R.id.verify_button);
        pinView = view.findViewById(R.id.pin_view);

        
        if(source.equals("SignUpFragment")){
            phoneNo = "+" + SignUpFragment.mobileTxt;

        }
        else if(source.equals("LoginFragment")){
            phoneNo = "+" + LoginFragment.mobileTxt;
        }
        else if(source.equals("ForgotPasswordFragment")){
            phoneNo = "+" + ForgotPasswordFragment.mobileTxt;
        }
        otpMobileTextView.setText("Enter OTP sent to Your Phone" + "\n" + phoneNo);

        backArrowImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.verifyPhone_to_login);
            }
        });
        backTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.verifyPhone_to_login);
            }
        });

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNo,
                60,
                TimeUnit.SECONDS,
                getActivity(),
                new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
                    @Override
                    public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                        VerificationCodSent = phoneAuthCredential.getSmsCode();
                        if(VerificationCodSent != null){
                            progressBar.setVisibility(View.VISIBLE);
                            verifyButton.setVisibility(view.INVISIBLE);

                            PhoneAuthCredential phoneAuthCredential1 = PhoneAuthProvider.getCredential(
                                    VerificationCodSent,
                                    CodeEnteredByUser
                            );
                            FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    progressBar.setVisibility(View.GONE);
                                    verifyButton.setVisibility(View.VISIBLE);
                                    if(task.isSuccessful()){
                                        verified = true;
                                    }
                                    else{
                                        Toast.makeText(getContext(), "the Verification Code entered was invalid", Toast.LENGTH_LONG).show();
                                        verified = false;
                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void onVerificationFailed(@NonNull FirebaseException e) {
                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCodeSent(@NonNull String VerificationCodeSent, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        VerificationCodSent = VerificationCodeSent;
                    }
                }
        );

        verifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pinView.getText().toString().isEmpty()){
                    Toast.makeText(getContext(), "Please enter a valid verification code", Toast.LENGTH_LONG).show();
                    verified = false;
                    goToAppropriateFragment(source);
                }

                CodeEnteredByUser = pinView.getText().toString();

                if(VerificationCodSent != null){
                    progressBar.setVisibility(View.VISIBLE);
                    verifyButton.setVisibility(view.INVISIBLE);

                    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
                            VerificationCodSent,
                            CodeEnteredByUser
                    );
                    FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            progressBar.setVisibility(View.GONE);
                            verifyButton.setVisibility(View.VISIBLE);
                            if(task.isSuccessful()){
                                verified = true;
                            }
                            else{
                                Toast.makeText(getContext(), "the Verification Code entered was invalid", Toast.LENGTH_LONG).show();
                                verified = false;
                            }
                        }
                    });
                }

                // afterVerification
                goToAppropriateFragment(source);
            }
        });

    }

    public void goToAppropriateFragment(String source){

        final NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);

        if(source.equals("SignUpFragment")){
            if(verified){
                DatabaseHelper databaseHelper = new DatabaseHelper(getContext());

                if(databaseHelper.addNewUser()){ // if the user added successfully addUser() returns true
                    Toast.makeText(getContext(), "You have signed up successfully! you can login", Toast.LENGTH_LONG).show();
                    // go to LoginFragment
                    navController.navigate(R.id.verifyPhone_to_login);
                }
                else{ // to be deleted won't  arrive here if email is already used
                    Toast.makeText(getContext(), "Sorry, Unable to sign you up! Try again later", Toast.LENGTH_LONG).show();
                }
            }
            else {
                // Toast.makeText(getContext(), "Sorry, it seems that you have not entered the OTP correctly. Try again", Toast.LENGTH_LONG).show();
                // go to SignUpFragment
                navController.navigate(R.id.verifyPhone_to_signUp);
            }
        }
        else if(source.equals("LoginFragment")){
            if(verified){
                // go to MainContentActivity
                Toast.makeText(getContext(), "Welcome back " + LoginFragment.fNameTxt + "!", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getContext(), MainContentActivity.class);
                startActivity(intent);
            }
            else {
                // Toast.makeText(getContext(), "Sorry, it seems that you have not entered the OTP correctly. Try again", Toast.LENGTH_LONG).show();
                // go to LoginFragment
                navController.navigate(R.id.verifyPhone_to_login);
            }
        }
        else if(source.equals("ForgotPasswordFragment")){
            if(verified){
                // go to ResetPasswordFragment
                navController.navigate(R.id.verifyPhone_to_resetPassword);
            }
            else {
                Toast.makeText(getContext(), "Sorry, it seems that you have not entered the OTP correctly. Try again", Toast.LENGTH_LONG).show();
                // go to LoginFragment
                navController.navigate(R.id.verifyPhone_to_login);
            }
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_verify_phone, container, false);
    }
}

日志:

2021-03-30 20:57:21.769 16847-16847/com。实例map_project_v2 E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度2021-03-30 20:57:50.049 16847-16847/com。实例地图项目2 E/zzf:检索安全网令牌时出现问题:7:2021-03-30 20:57:51.267 16847-17116/com。实例map_project_v2 E/FirebaseAuth:[GetAuthDomainTask]获取项目配置时出错。失败,证书哈希400 2021-03-30 20:57:51.381 16847-16847/com无效。实例map_project_v2 E/zzf:无法获取reCAPTCHA令牌,出现错误[尝试获取包证书哈希时出错。]-未经应用验证呼叫后端2021-03-30 20:57:51.763 16847-16976/com。实例map_project_v2 E/FirebaseAuth:[SmsRetrieverHelper]SMS验证码请求失败:未知状态码:17093 null

共有1个答案

卓星波
2023-03-14

我遇到了同样的错误,我可以通过以下最后两个步骤解决它(确保您已经涵盖了所有这些步骤):

  1. 在Firebase身份验证下的登录方法中启用电话选项
  2. 确保下载并添加最新的谷歌服务。项目中的json文件
  3. 在中为firebase项目启用Android设备验证https://console.cloud.google.com/
  4. 添加库实现“androidx.browser:browser:1.3.0”https://developer.android.com/jetpack...
 类似资料:
  • 当使用Firebase Auth进行身份验证时,我想自动输入通过SMS接收的代码。我能够接收短信并手动完成身份验证过程,但当我使用SmsRetriever时,应用程序崩溃,然后显示底部工作表对话框。这是Logcat中显示的所有内容: [SmsRetrieverHelper]SMS校验码请求失败:未知状态代码:17010 null 用户输入其电话号码的代码片段: 这是片段中的代码,用户必须在其中输入

  • 我使用包括Spring Security和Vaadin在内的https://start.spring.io设置了一个Spring Boot项目。然后我将Vaadin版本设置为22.0.4,并按照本教程使用Vaadin Flow和Spring Security设置登录页面:https://vaadin.com/docs/v22/flow/tutorial/login-and-authenticati

  • 问题内容: 我需要使用PostForm方法将代理与auth一起使用。如果我使用类似(简体)的内容: 我可以轻松做到,并且效果很好。但是现在,我正在编辑第三方程序包,并尝试将代理添加到现有代码中: 在我看来,它是行不通的,而且失败了。在此示例中,没有身份验证的代理可以正常工作。有人知道吗,在这种情况下我可以在auth中使用代理吗? 问题答案: 您正在尝试向响应中添加标头,这不是您发送到服务器的内容,

  • 我正在使用生成用户密码的强哈希值。我想登录用户,但不想通过网络以明文形式发送密码,如何检查密码是否正确(没有往返),因为它是加盐的? 我有一个客户端/服务器场景。客户端是台式计算机上的应用程序(不是网站,也不是超文本传输协议服务器)。 我怎样才能做到这一点?我只是走了这么远:我正在客户端上生成salt散列,从中形成一个mcf并将其发送到我的服务器。将mcf保存到数据库。我没有发送密码,只发送了实际

  • 是否可以在Flatter中使用用户名和密码(而不是电子邮件和密码)实现Firebase身份验证?有没有办法用Firebase Auth插件来实现这一点?