Search code examples
firebasexamarin.androidadmin

Give firebase admin rights to xamarin application


In firebase console I can get this source code for java and some adminsdk json file to download. However I'm working in c# xamarin and it doesn't have the setCredential() function. This is java code that I can download in firebase console

FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://abc-def.firebaseio.com")
.build();

FirebaseApp.initializeApp(options);

But in xamarin, there is no SetCredential function. I can do only things like:

 FirebaseOptions options = new FirebaseOptions.Builder()
     .SetApplicationId("1:3465124113567:android:81db52b2352255cd")
     .SetDatabaseUrl("https://abc-def.firebaseio.com/")
     .SetStorageBucket("gs://abc-def.appspot.com")
     .SetApiKey("???")
     .SetGcmSenderId("???")
     .Build();
 var firebaseApp = FirebaseApp.InitializeApp(context, options);

How can I use that file? I made couple of tries in SetApiKey, setting some lines from that file, but somehow the application is just crashing on InitializeApp line, without meaningfull explanation in output window. How is it done that my application gets admin privileges in c# - xamarin?

In this question I found that SetCredentials wasn't there always, but all my xamarin.firebase.xxx components are on version 10.2.1 so this shouldn't be problem.


Solution

  • Refer to @Doug Stevenson 's answer, you are using the Firebase Android client library, not the Firebase Admin SDK. You could read the javadoc for FirebaseOptions.Builder in the Android client library :

    Android : FirebaseOptions.Builder

    Then look at the same class from the java Admin SDK (note the URL is different) :

    Admin SDK : FirebaseOptions.Builder

    They are different things, in your project you are using the Android SDK definition and not the admin SDK definition.