若在使用 Firebase 安全认证(AppCheck)时出现了“Native Android Firebase AppCheck App attestation failed”的错误,这通常表示未应用正确的 App Attestation 密钥,可能是因为证书被篡改或者 OpenSSL 版本有问题。解决此问题的步骤如下:
FirebaseApp.initializeApp(
context,
new FirebaseOptions.Builder()
.setProjectId("{your-project}")
.setApiKey("{your-api-key}")
.setApplicationId("{your-app-id}")
.build());
FirebaseApp.getInstance().setTokenProvider(new AppCheckFirebaseTokenProvider.Builder()
.setAppCheckTokenListener(
new AppCheckFirebaseTokenListener() {
@Override
public void onTokenChanged(@NonNull TokenResult tokenResult) {
// Forward the token to your backend for verification
}
@Override
public void onTokenError(@NonNull Exception e) {
// Handle error
}
})
.build());
android {
[...]
ndk {
abiFilters 'armeabi-v7a', 'x86', 'arm64-v8a', 'x86_64'
}
[...]
}
dependencies {
[...]
implementation 'com.google.firebase:firebase-appcheck-safetynet:17.0.0'
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.9.0'