繁体   English   中英

firebase认证出现问题,使用android studio,代码未发送

[英]Problem in firebase authentication, using android studio , code not sent

我在 firebase 项目中包含了 SHA1 和 SHA-256。

当片段运行时出现异常 Toast 消息:“此请求缺少有效的应用程序标识符,这意味着 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.example.map_project_v2 E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE 跨度不能为零长度 2021-03-30 20:57:50.049 16847-16847/com.example.map_project_v2 E /zzf:检索 SafetyNet 令牌时出现问题:7:2021-03-30 20:57:51.267 16847-17116/com.example.map_project_v2 E/FirebaseAuth:[GetAuthDomainTask] 获取项目配置时出错。 Failed with INVALID_CERT_HASH 400 2021-03-30 20:57:51.381 16847-16847/com.example.map_project_v2 E/zzf: Failed to get reCAPTCHA token with error [There was an error while trying to get your package certificate hash.]-在没有应用验证的情况下调用后端 2021-03-30 20:57:51.763 16847-16976/com.example.map_project_v2 E/FirebaseAuth:[SmsRetrieverHelper] SMS 验证码请求失败:未知状态码:17093 null

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

  1. 在 Firebase 身份验证下的登录方法中启用电话选项
  2. 确保在您的项目中下载并添加最新的 google-services.json 文件
  3. https://console.cloud.google.com/中为您的 firebase 项目启用 Android 设备验证
  4. 添加库实现“androidx.browser:browser:1.3.0” https://developer.android.com/jetpack..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM