Skip to content

Instantly share code, notes, and snippets.

View hiranya911's full-sized avatar

Hiranya Jayathilaka hiranya911

View GitHub Profile
@hiranya911
hiranya911 / ADCExample.java
Last active November 26, 2017 22:56
Firebase Java Admin SDK - Init with ADC
public static void initWithAppDefaultCredentials() {
// ADC does not require any explicit credential files.
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.build();
FirebaseApp.initializeApp(options);
}
public static void initWithServiceAccountCredentials() {
// Service account must be specified explicitly.
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
FirebaseApp.initializeApp(options);
}
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid"
}
}
}
}
public static void initWithLimitedPrivileges() {
// Initialize the app with a custom auth variable, limiting the server's access
Map<String, Object> auth = new HashMap<String, Object>();
auth.put("uid", "my-service-worker");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://databaseName.firebaseio.com")
.setDatabaseAuthVariableOverride(auth)
.build();
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
@Deprecated
public void addTaskCallbacks(Task<String> task) {
task.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String result) {
System.out.println("Operation completed with result: " + result);
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
@Deprecated
public Task<List<String>> continueWithTask(Task<String> task) {
return task.continueWith(new Continuation<String, List<String>>() {
@Override
public List<String> then(Task<String> task) throws Exception {
String result = task.getResult();
import com.google.api.core.ApiFutures;
import com.google.firebase.tasks.Tasks;
String value = "value";
Exception error = new Exception("custom error");
// Wrap in Tasks
Task<String> valueTask = Tasks.forResult(value);
Task<String> exceptionTask = Tasks.forException(error);
import com.google.firebase.tasks.Tasks;
@Deprecated
public String waitForTask(Task<String> task) {
try {
return Tasks.await(task);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException("Error while waiting for task", e);
}
}
import com.google.api.core.SettableApiFuture;
import com.google.firebase.tasks.TaskCompletionSource;
@Deprecated
public Task<String> incompleteTask() {
final TaskCompletionSource<String> source = new TaskCompletionSource<>();
new Thread() {
@Override
public void run() {
try {
// Start listing users from the beginning, 1000 at a time.
ListUsersPage page = FirebaseAuth.getInstance().listUsersAsync(null).get();
while (page != null) {
for (ExportedUserRecord user : page.getValues()) {
System.out.println("User: " + user.getUid());
}
page = page.getNextPage();
}
// Iterate through all users. This will still retrieve users in batches,