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

Android Studio中的Firebase手机验证错误

云文栋
2023-03-14

我收到了电话身份验证错误。我成功地收到了OTP,但是在我与GitHub的团队成员共享我的项目后,我收到了这个错误。

我还添加了我的SHA 1

我还想通知您,在将我的项目转移到GitHub后,我创建了新的Firebase帐户并添加了SHA 1

Logcat:

03-16 16:19:40.001 8163-8163/com.roundtripride E/zzf: Problem retrieving SafetyNet Token: 7: 
03-16 16:19:40.679 8163-8235/com.roundtripride E/FirebaseAuth: [GetAuthDomainTask] Error getting project config. Failed with INVALID_CERT_HASH 400
03-16 16:19:40.713 8163-8163/com.roundtripride E/zzf: Failed to get reCAPTCHA token with error [There was an error while trying to get your package certificate hash.]- calling backend without app verification
03-16 16:19:41.202 8163-8201/com.roundtripride E/FirebaseAuth: [SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17093 null
03-16 16:19:41.202 8163-8163/com.roundtripride E/Tag: onVerificationFailed : This request is missing a valid app identifier, meaning that neither SafetyNet checks nor reCAPTCHA checks succeeded. Please try again, or check the logcat for more details.
03-16 16:19:41.202 8163-8163/com.roundtripride E/Tag: onVerificationFailed : This request is missing a valid app identifier, meaning that neither SafetyNet checks nor reCAPTCHA checks succeeded. Please try again, or check the logcat for more details.
03-16 16:19:41.202 8163-8163/com.roundtripride E/Tag: onVerificationFailed : com.google.firebase.auth.FirebaseAuthException: This request is missing a valid app identifier, meaning that neither SafetyNet checks nor reCAPTCHA checks succeeded. Please try again, or check the logcat for more details.

依赖项:

dependencies {
 implementation fileTree(dir: "libs", include: ["*.jar"])
 implementation 'androidx.appcompat:appcompat:1.2.0'
 implementation 'com.google.android.material:material:1.3.0'
 implementation 'com.google.android.gms:play-services-maps:17.0.0'
 implementation 'com.google.android.gms:play-services-location:18.0.0'
 implementation 'com.google.android.libraries.places:places:2.4.0'
 implementation platform('com.google.firebase:firebase-bom:26.7.0')
 implementation 'com.google.firebase:firebase-analytics'
 implementation 'com.google.firebase:firebase-auth:20.0.3'
 testImplementation 'junit:junit:4.13.2'
 androidTestImplementation 'androidx.test.ext:junit:1.1.2'
 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
 implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
 implementation 'com.intuit.sdp:sdp-android:1.0.6'
 implementation 'de.hdodenhof:circleimageview:3.1.0'
 implementation 'com.amitshekhar.android:jackson-android-networking:1.0.2'
 implementation 'com.github.prolificinteractive:material-calendarview:1.6.0'
 implementation 'androidx.browser:browser:1.3.0'
 implementation 'com.google.firebase:firebase-messaging'
 implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
}

代码:

public class OTPVerifyActivity extends BaseActivity implements View.OnClickListener {

 Context context;
 private FirebaseAuth mAuth;
 private String mVerificationId;
 String idToken = "", mobile;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_otp_verify);
    Utility.setLoginStatusColor(this);

    context = this;
    txt_finish = findViewById(R.id.txt_finish);
    edt_1 = findViewById(R.id.edt_1);

    
    mAuth = FirebaseAuth.getInstance();
    mobile = getIntent().getStringExtra("mobile");
    sendVerificationCode();

    txt_finish.setOnClickListener(this);
}


@Override
public void onClick(View view) {
    if (view == txt_finish) {
        Utility.hideSoftKeyboard(edt_1, context);
        if (edt_1.getText().toString().equals("")) {
            Utility.errDialog("Please enter OTP", context);
        } else {
            verifyVerificationCode(edt_1.getText().toString().trim());
        }
    }
}

private void sendVerificationCode() {
    PhoneAuthOptions options =
            PhoneAuthOptions.newBuilder(mAuth)
                    .setPhoneNumber("+91" + mobile)       // Phone number to verify
                    .setTimeout(30L, TimeUnit.SECONDS) // Timeout and unit
                    .setActivity(this)                 // Activity (for callback binding)
                    .setCallbacks(mCallbacks)          // OnVerificationStateChangedCallbacks
                    .build();
    PhoneAuthProvider.verifyPhoneNumber(options);
}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
        Log.e("Tag", "get Data : " + phoneAuthCredential.getSmsCode());
        Utility.dismissProgressDialog(pd);
        String code = phoneAuthCredential.getSmsCode();
        if (code != null) {
            edt_1.setText(code);
            verifyVerificationCode(code);
        }
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        Utility.dismissProgressDialog(pd);
        Log.e("Tag", "onVerificationFailed : " + e.getMessage());
        Log.e("Tag", "onVerificationFailed : " + e.getLocalizedMessage());
        Log.e("Tag", "onVerificationFailed : " + e.toString());
        Utility.errDialog(e.getMessage(), context);
    }

    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(s, forceResendingToken);
        mVerificationId = s;
        Utility.dismissProgressDialog(pd);
    }
};

private void verifyVerificationCode(String code) {
    //Log.e("Tag","Verify code : "+code);
    pd = Utility.showProgressDialog(context);
    try {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);
        signInWithPhoneAuthCredential(credential);
    } catch (Exception e) {
        Log.e("Tag", "PhoneAuthCredential Exception " + e.getMessage());
    }
}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(OTPVerifyActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        user.getIdToken(true)
                                .addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                                    public void onComplete(@NonNull Task<GetTokenResult> task) {
                                        if (task.isSuccessful()) {
                                            idToken = task.getResult().getToken();
                                            Utility.dismissProgressDialog(pd);
                                            Log.e("Tag", "Success OTP " + isDriver);
                                            MyPreference.setPreferenceValue(context,"isLogin","true");
                                           
                                        }
                                    }
                                });
                    } else {
                        Utility.dismissProgressDialog(pd);
                        //verification unsuccessful.. display an error message
                        String message = "Something is wrong, we will fix it soon...";
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            message = "Invalid code entered...";
                        }
                        Log.e("Tag", message);
                        Utility.errDialog(message, context);
                    }
                }
            });
  }

}

共有2个答案

娄嘉石
2023-03-14

androidx的实现。浏览器:浏览器:1.3.0'

添加此依赖项,然后再次检查sha1和SH256,然后重试

公良鸿禧
2023-03-14

>

  • 从旧的Firebase帐户中删除调试SHA-1和SHA-256。

    打开android studio,点击右角的gradle

    在新firebase帐户中添加SHA1和SHA-256。(如果您是从2系统构建apk,请同时添加系统的SHA1和SHA-256)。

    您需要确保在Firebase控制台中启用了电话认证=

    在构建中添加依赖项。gradle(:app)

    androidx的实现。浏览器:浏览器:1.3.0'

    进入谷歌云控制台,选择你的项目。

    单击导航菜单并选择API

    点击启用api和服务,启用api“Android设备验证”。

    下载并替换项目中最新的google-services.json文件

  •  类似资料:
    • 我已经在我现有的android项目上附加了firebase,并运行了它。这是一个场景:如果应用程序通过开发者模式从android studio运行,它(通过电话号码登录Firebase)运行平稳,我也能成功登录。但是,每当我构建签名的调试apk,Firebase都不允许登录。它给出了以下信息: 此应用程序未被授权使用Firebase身份验证。请验证firebase控制台中配置了正确的包名称和SHA

    • X2.3.0新增(暂时无法使用) sp_check_mobile_verify_code($mobile='',$verifycode='') 功能: 手机验证码检查,验证完后销毁验证码增加安全性 参数: $mobile: 手机号 $verifycode:验证码 返回: 类型boolean true:手机验证码正确,false:手机验证码错误 使用: $is_right_mobile_code

    • 我想用SafetyNet验证电话号码Firebase,而不是重新CAPTCHA验证。我在这里遵循Firebase留档:https://firebase.google.com/docs/auth/android/phone-auth 在谷歌API控制台中,我启用了Android设备验证API 在Firebase控制台中,我添加了SHA-256指纹 重新安装谷歌服务。json并将其添加到项目中 它总是

    • 我正在寻找一种方法来查找从Firebase电话身份验证发送给我的代码,因为我想手动验证该代码。现在的问题是Firebase正在自动检测短信并调用onVerficationComplted(),但我有一个按钮,我想手动输入otp代码并验证。下面是我的代码。如果有任何帮助,将不胜感激。谢谢

    • 接口说明 用来验证输入的手机号是否已注册 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /api/user/1.0.0/validatePhone 是否需要登录 否 请求字段说明 参数 类型 请求类型 是否必须 说明 phone string form 是 手机号 响应字段说明 参数 类型 说明 phoneVali String

    • 可用于强制重新发送SMS验证码。然而,在对医生做了一些研究之后,我仍然不知道该怎么做。