Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Migration to GoogleCredentials and ApiFutures #1

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion admin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ repositories {

dependencies {
// Firebase Admin Java SDK
compile 'com.google.firebase:firebase-admin:5.1.0'
compile 'com.google.firebase:firebase-admin:5.3.1'
}
34 changes: 10 additions & 24 deletions admin/src/main/java/com/google/firebase/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,25 @@

package com.google.firebase.example;

import com.google.firebase.auth.FirebaseCredential;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.auth.GoogleOAuthAccessToken;
import com.google.firebase.internal.NonNull;
import com.google.firebase.tasks.OnCompleteListener;
import com.google.firebase.tasks.Task;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {

public void getServiceAccountAccessToken() throws FileNotFoundException, IOException {
public void getServiceAccountAccessToken() throws IOException {
// https://firebase.google.com/docs/reference/dynamic-links/analytics#api_authorization
// [START get_service_account_tokens]
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
FirebaseCredential credential = FirebaseCredentials.fromCertificate(serviceAccount);
credential.getAccessToken().addOnCompleteListener(
new OnCompleteListener<GoogleOAuthAccessToken>() {
@Override
public void onComplete(@NonNull Task<GoogleOAuthAccessToken> task) {
if (task.isSuccessful()) {
String accessToken = task.getResult().getAccessToken();
long expirationTime = task.getResult().getExpiryTime();
// Attach accessToken to HTTPS request in the
// "Authorization: Bearer" header
// After expirationTime, you must generate a new access
// token
} else {
// Error
}
}});
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
credentials.refresh();
String accessToken = credentials.getAccessToken().getTokenValue();
long expirationTime = credentials.getAccessToken().getExpirationTime().getTime();
// Attach accessToken to HTTPS request in the
// "Authorization: Bearer" header
// After expirationTime, you must generate a new access
// token
// [END get_service_account_tokens]
}

Expand Down
2 changes: 1 addition & 1 deletion database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ repositories {

dependencies {
// Firebase Java SDK
compile 'com.google.firebase:firebase-admin:5.1.0'
compile 'com.google.firebase:firebase-admin:5.3.1'
}
30 changes: 13 additions & 17 deletions database/src/main/java/com/google/firebase/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
package com.google.firebase.example;

import com.google.firebase.database.*;
import com.google.firebase.internal.NonNull;
import com.google.firebase.tasks.OnCompleteListener;
import com.google.firebase.tasks.Task;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
Expand Down Expand Up @@ -94,45 +90,45 @@ public void savingData() {
// [START set_user_data_all]
DatabaseReference usersRef = ref.child("users");

Map<String, User> users = new HashMap<String, User>();
Map<String, User> users = new HashMap<>();
users.put("alanisawesome", new User("June 23, 1912", "Alan Turing"));
users.put("gracehop", new User("December 9, 1906", "Grace Hopper"));

usersRef.setValue(users);
usersRef.setValueAsync(users);
// [END set_user_data_all]

// [START set_user_data_child]
usersRef.child("alanisawesome").setValue(new User("June 23, 1912", "Alan Turing"));
usersRef.child("gracehop").setValue(new User("December 9, 1906", "Grace Hopper"));
usersRef.child("alanisawesome").setValueAsync(new User("June 23, 1912", "Alan Turing"));
usersRef.child("gracehop").setValueAsync(new User("December 9, 1906", "Grace Hopper"));
// [END set_user_data_child]

// [START single_user_update_children]
DatabaseReference hopperRef = usersRef.child("gracehop");
Map<String, Object> hopperUpdates = new HashMap<String, Object>();
Map<String, Object> hopperUpdates = new HashMap<>();
hopperUpdates.put("nickname", "Amazing Grace");

hopperRef.updateChildren(hopperUpdates);
hopperRef.updateChildrenAsync(hopperUpdates);
// [END single_user_update_children]

// [START multi_user_update_children]
Map<String, Object> userUpdates = new HashMap<String, Object>();
Map<String, Object> userUpdates = new HashMap<>();
userUpdates.put("alanisawesome/nickname", "Alan The Machine");
userUpdates.put("gracehop/nickname", "Amazing Grace");

usersRef.updateChildren(userUpdates);
usersRef.updateChildrenAsync(userUpdates);
// [END multi_user_update_children]

// [START multi_user_object_updates]
Map<String, Object> userNicknameUpdates = new HashMap<String, Object>();
Map<String, Object> userNicknameUpdates = new HashMap<>();
userNicknameUpdates.put("alanisawesome", new User(null, null, "Alan The Machine"));
userNicknameUpdates.put("gracehop", new User(null, null, "Amazing Grace"));

usersRef.updateChildren(userNicknameUpdates);
usersRef.updateChildrenAsync(userNicknameUpdates);
// [END multi_user_object_updates]

// [START adding_completion_callback]
DatabaseReference dataRef = ref.child("data");
dataRef.setValue("I'm writing data", new DatabaseReference.CompletionListener() {
dataRef.setValueAsync("I'm writing data", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
Expand All @@ -149,10 +145,10 @@ public void onComplete(DatabaseError databaseError, DatabaseReference databaseRe
DatabaseReference postsRef = ref.child("posts");

DatabaseReference newPostRef = postsRef.push();
newPostRef.setValue(new Post("gracehop", "Announcing COBOL, a New Programming Language"));
newPostRef.setValueAsync(new Post("gracehop", "Announcing COBOL, a New Programming Language"));

// We can also chain the two calls together
postsRef.push().setValue(new Post("alanisawesome", "The Turing Machine"));
postsRef.push().setValueAsync(new Post("alanisawesome", "The Turing Machine"));
// [END push_posts]

// [START getting_post_id]
Expand Down