I'm hosting an app written in Kotlin on AppEngine and have come across overlapping documentation from Google regarding Authenticating a Firestore running on a server. Both implementations run as expected on AppEngine. Does one implementation have advantages, are there separate use cases, or is this purely a duplication?
Initialize on your own server To use the Firebase Admin SDK on your own server, use a service account.
Go to IAM & admin > Service accounts in the Cloud Platform Console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK:
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
// Use a service account
InputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.build();
FirebaseApp.initializeApp(options);
Firestore db = FirestoreClient.getFirestore();
Add the server client library to your app
Using Gradle:
compile 'com.google.cloud:google-cloud-firestore:0.58.0-beta'Initialize Cloud Firestore
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
FirestoreOptions firestoreOptions =
FirestoreOptions.getDefaultInstance().toBuilder()
.setProjectId(projectId)
.build();
Firestore db = firestoreOptions.getService();
The Firebase Admin SDK (which bundles SDKs for a bunch of different products) is just a wrapper around the Google Cloud Firestore SDK. Pick the one that's most convenient for your situation. - Doug Stevenson, Firebase team
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With