【问题标题】:Firebase phone authentication error in Android StudioAndroid Studio 中的 Firebase 手机身份验证错误
【发布时间】:2021-03-16 11:14:07
【问题描述】:

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

我还在新的 Firebase 控制台中添加了 MY SHA 1 和 SHA 256。我还添加了依赖项,但仍然出现此错误。

我还想通知,在将我的项目转移到 GitHub 后,我创建了新的 firebase 帐户并在此项目中添加 SHA 1 和 SHA 256

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);
                    }
                }
            });
  }

}

【问题讨论】:

  • 请分享你的logcat
  • prnt.sc/10n6h4q我的logcate截图链接
  • 发送otp后会打开浏览器进行应用验证
  • 看起来你拒绝了它
  • 或者请检查谷歌服务是否替换。

标签: android firebase firebase-authentication


【解决方案1】:
  1. 从您的旧 Firebase 帐户中删除您的调试 SHA-1 和 SHA-256。

  2. 打开 android studio 并点击右上角的 gradle > 点击你的项目 > 选择应用 > 选择任务 > 选择 android > 点击签署报告 > 从那里复制我们的 SHA1 和 SHA-256。

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

  4. 您需要确保在 Firebase 控制台中启用了电话身份验证 => 身份验证 => 登录方法。

  5. 在 build.gradle(:app) 中添加依赖

    实现'androidx.browser:browser:1.3.0'

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

  7. 点击导航菜单并选择 APIs & services 然后选择 Dashboard 。

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

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

【讨论】:

    【解决方案2】:

    实现'androidx.browser:browser:1.3.0'

    请添加此依赖项,然后再检查一次 sha1 和 sha256,然后尝试一下

    【讨论】:

    • 我已经添加了它,我也在我的问题中提到了它
    • 请从第一个 Firebase 帐户中删除应用
    • 确保 sha1 和 sha256 在用于构建 apk 的同一设备上生成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多