Reputation: 276
I want to include Firebase App Check for Firebase Storage in my Android Flutter App. Therefore I was following the official documentation: https://firebase.flutter.dev/docs/app-check/usage.
This is my Kotlin MainActivity:
import android.os.Bundle
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
FirebaseApp.initializeApp(/*context=*/ this);
val firebaseAppCheck = FirebaseAppCheck.getInstance()
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance())
super.onCreate(savedInstanceState)
}
}
and this is my main():
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate();
runApp(MyApp());
}
I also added this to my app/build.gradle
dependencies {
implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta01'
}
When I make a request to firebase storage, I would expect something like this in my console:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
Instead, I'm getting an error:
Error getting App Check token; using placeholder token instead. Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: Requests from this Android client application are blocked.
Did I miss something here? I am using a real Android device with flutter debug build.
Upvotes: 14
Views: 8734
Reputation: 123
[POSSIBLE SOLUTION] For anyone new to Koltin like me looking for a solution, the logs won't appear on the Run tab when you run your flutter project, you need to open android studio's Log Cat
and type AppCheck
to get the logs.
Also make sure to follow the documentation for installing Appcheck for kotlin.
*NOTE: I'm using an emulator.
*Note if you can't find the tab Log Cat
please visit Enable Log Cat Flutter
Upvotes: 4
Reputation: 41
I had exactly the same problem in one of my projects for an Android app. I found the solution and I'm sure it's a solution for flutter apps too.
If you have done all the steps correctly to set up the application for App Check, then install the application in debug mode.
In the logs of the application you will find the secret debug key. You should enter this key in your firebase project in the App Check tab in the debug tokens manager.
Attention This key has been valid for as long as you have set in the App Check settings.
You have to wait a few minutes.
Now in your project you should remove the code that generates the debug key and replace it with the normal code.
Remove this
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance()
)
Add this
firebaseAppCheck.installAppCheckProviderFactory(
PlayIntegrityAppCheckProviderFactory.getInstance()
)
Now everything should work normally!
The problem is simple! If you leave the code that generates the new key, you generate a new key, thus invalidating the previous one.
I hope I helped!
Footnote
I spent 2 days and nights to find the solution, at one point I thought I was dumb
Upvotes: 0
Reputation: 1
You have to initialize DebugAppCheckProviderFactory
before main activity onCreate
done. This works for me.
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
FirebaseApp.initializeApp(/*context=*/ this);
val firebaseAppCheck = FirebaseAppCheck.getInstance()
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance())
super.onCreate(savedInstanceState)
}
}
Note that this code should be put in debug mode only. You might want to exclude it from release build.
Upvotes: -1
Reputation: 11941
Try to download your google-services.json again.
If this does not help, might need to add/re-add your debug key's SHA-1 certificate
after doing so, get new google-services.json
Apparently being blocked
by firebase means your API key is configured improperly.
If you are getting this error:
Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: App attestation failed.
Check your SafetyNet provider in the Project Settings > App Check
.
You will need to provide SHA-256 fingerprint of your app's signing certificate. (would also suggest increasing token live time)
Upvotes: 2