9

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.

Florian
  • 204
  • 1
  • 9

2 Answers2

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)

Nuts
  • 8,789
  • 1
  • 31
  • 38
  • 2
    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. – Maciej Caputa Aug 03 '21 at 15:03
  • 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. :) – Maciej Caputa Aug 04 '21 at 11:21
  • 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? – Maciej Caputa Aug 07 '21 at 15:19
  • 2
    **[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 it – ABDERRAHMANE OUALI Dec 12 '21 at 21:41
-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.

  • 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. – Florian Sep 02 '21 at 11:51
  • Please make sure that `.MainActivity` is declared as your main activity name at `AndroidManifest.xml`: ` ... – Anh Tran Sep 03 '21 at 12:25