Can not generate a debug token for Firebase Storage with App Check
Asked Answered
T

4

13

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.

Tortious answered 8/7, 2021 at 10:27 Comment(3)
I have the same issue, were you able to resolve it?Humic
How did you solve this ?Sylvan
I don't even receive the printed log information... The doc is very poorSabrinasabsay
L
4

[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

See How it should look like

Longeron answered 13/4, 2023 at 7:40 Comment(0)
O
2

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)

Oral answered 3/8, 2021 at 14:33 Comment(4)
Thank you for your suggestion. I've just removed and added my debug and release SHA-1 keys to firebase and downloaded google-services.json again, but the same issue is printed when I want to call an https function: Error getting App Check token; using placeholder token instead. Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: App attestation failed.Humic
thank you for the update 1. I've removed all SHA-1 keys and added both my release and debug keys to firebase project. 2. Dowloaded google-service.json again and added it to my project. 3. I've added both SHA-256 keys (release and debug) to SafetyNet. 4. Why do you suggest to increase token live time? And to what do you suggest to increase it to? 5. I added a call to firebae callable function of app start and it works in debug mode on my real device. I will now deploy to production and see if it works. :)Humic
Update: Good thing is that I've managed to get this working on iOS but the problem still remains on Android. Interestingly, when I have a real device connected it works even without registering debug token. Do you have an idea what can I try?Humic
[SOLVED] I did generate SHA-256 key from the following comand cd android && ./gradlew signingReport then look for the appsigning with variant Debug and copied SHA-256 string into the firebase console Project setting > app check and clicked safety net and pasted the string into itDinin
E
0

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

Except answered 14/12, 2022 at 17:16 Comment(0)
S
-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.

Semipalatinsk answered 2/9, 2021 at 2:33 Comment(2)
Thank you, but this does not work for me either. But I have a feeling that it has something to do with my MainActivity.kt file. When I make a request to Firebase Storage, no AppCheckToken gets printed to my console. So I think this line firebaseAppCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance()) gets never executed to generate a new Debug-Token for me.Tortious
Please make sure that .MainActivity is declared as your main activity name at AndroidManifest.xml: ` ... <activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" `Semipalatinsk

© 2022 - 2025 — McMap. All rights reserved.