簡體   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