Firebase and Processing (Java)

Has anyone had success with integrating Processing and Firebase from google? I have been trying to figure out a way to do it but it so far has been too far beyond my reach. Theoretically, I think it should work since the SDK is Java but have been unable to compile it into a jar (tried maven, but fails testing) to access from processing.
Probably should have started with this, here is the type of project I am working on: Windows 10 PC, touchscreen based client project workflow and resource allocation presentation/editor with 1 master display (large screen and touchscreen frame) and about 10 offsite users (web based). Client list is about 5000 (historical) and anticipate about 500 new clients/projects per year. Data storage is quite light, just address book references and project specific fields (I.e project number, staff assigned, dates, etc), would like to keep it to JSON format. Looking at hosted solution due to no IT staff. Since I can’t ensure I will be at the company forever, I wanted a solution with the least amount of risk to change and Firebase or AWS seems to be the most logical.
If anyone has suggestions or opinions, please respond. Also, looking for suggestions on touchscreen frames, what should I look for as far as specs goes?

By Maven, did you mean something like this? :confused:
Search.Maven.org/search?q=Firebase
Search.Maven.org/search?q=g:com.google.firebase

This is what I tried, installed Maven from https://maven.apache.org/ and downloaded the Firebase admin from https://github.com/firebase/firebase-admin-java

Went through the build process using “mvn package” in a shell hoping to get a usable jar out of it.

There’s a download option in the Maven site. AFAIK, we don’t need to deal w/ Maven ourselves! :woozy_face:

Maven is a build tool that handles dependency management for you. If you go the manual download route you have to download all the dependencies and their dependencies too!

Maven does have a command for just downloading required JARs (which I can’t remember off the top of my head) or you could look at Apache Ivy which just does this.

1 Like

Thanks. Will try it tonight.

@William3954 – were you able to get this working?

Not completely yet. Was able to find the jars (links in previous post required “https”). Have been working on trying to get authentication to work, so far no luck. I am to the point where the samples java files from the firebase admin sdk will run, just not getting any valid responses yet from firebase; they have all been either invalid request or invalid token if I remember correctly.

I did find that firebase has a REST api which is great if you have full read/write open but authentication still has been an issue. Will be working on it more this week as this a side project at work and will post a guide once I figure it out. If you have more info on your end, please feel free to respond or pm me.
Cheers.

1 Like

Here is the code I am using for authentication via username/password to do authenticated REST request for firebase. Most of it is from a post on stack overflow: https://stackoverflow.com/questions/40824660/firebase-user-authentication-for-java-application-not-android

Please note, I have not checked to see if this code is secure in any way, use at your own risk. For the example, storing the username and password as hardcoded strings, if your are using this for distribution, use a different system for them.

Not sure if all of the jars or imports are needed, but it is what I had in my folder. Jars can be downloaded from https://www.dropbox.com/sh/ll117noujxyxh5x/AACBv4OzghdsbN8h-46u1M8Ya?dl=0
You will also need to have a user setup in the firebase console and allow username/password as an authentication method.

Please let me know if there are suggestions for improvements or other way of doing this. Will be working on implementing the REST api soon and will post when progress has been made. Have the admin module working as well so users can be managed on the fly but that is for a later time.

Cheers.

//Jars to inlcude:
/*
firebase-admin-6.8.1.jar
google-api-client-1.30.1.jar
google-auth-library-appengine-0.16.2.jar
google-auth-library-credentials-0.16.2.jar
google-auth-library-oauth2-http-0.16.2.jar
gson-2.8.5.jar
*/

//Change variables below to match your project
String username = "email@address.com";
String password = "God";
String ProjectName = "ProjectName"; //i.e. https://ProjectName.firebaseio.com/ 
String fireKey = "Web API Key";

import com.google.firebase.auth.FirebaseAuth;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

void setup() {
  size(200, 200);

  FireBaseAuth myFireAuth = new FireBaseAuth(fireKey); //Initialize Firebase
  try {
    String myToken= myFireAuth.auth(username, password); //Request token for username/password combo
    //Note: Tokens are valid for 1 hour (3600 seconds) after issuance
    myFireAuth.getUserPosts(myToken, "user-posts"); //Grabs top level data from "https://ProjectName.firebaseio.com/user-posts"
  }  
  catch (Exception e) {
    println(e);
  }
  
}

void draw() {
  background(45, 77, 131);
}


public class FireBaseAuth {
  private static final String BASE_URL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/";
  private static final String OPERATION_AUTH = "verifyPassword";
  private static final String OPERATION_REFRESH_TOKEN = "token";
  private static final String OPERATION_ACCOUNT_INFO = "getAccountInfo";
  private String MyTokenString;
  private String firebaseKey;
  private  FireBaseAuth instance = null;
  protected FireBaseAuth(String fireKeyIn) {
    firebaseKey = fireKeyIn;
  }

  public  FireBaseAuth getInstance() {
    if (instance == null) {
      instance = new FireBaseAuth(firebaseKey);
    }
    return instance;
  }

  public String auth(String username, String password) throws Exception { 
    HttpURLConnection urlRequest = null;
    String token = null;

    try {
      URL url = new URL(BASE_URL+OPERATION_AUTH+"?key="+firebaseKey);
      urlRequest = (HttpURLConnection) url.openConnection();
      urlRequest.setDoOutput(true);
      urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
      OutputStream os = urlRequest.getOutputStream();
      OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
      osw.write("{\"email\":\""+username+"\",\"password\":\""+password+"\",\"returnSecureToken\":true}");
      osw.flush();
      osw.close();
      urlRequest.connect();
      JsonParser jp = new JsonParser(); //from gson
      JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent())); //Convert the input stream to a json element
      JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 
      //println(rootobj);
      token = rootobj.get("idToken").getAsString();
    } 
    catch (Exception e) {
      println("auth Exception: " + e);
      return null;
    } 
    finally {
      urlRequest.disconnect();
    }
    return token;
  }


  void getUserPosts(String token, String DataSetToGrab) throws Exception {
    HttpURLConnection urlRequest = null;
    try {
      URL url = new URL("https://"+ProjectName+".firebaseio.com/" + DataSetToGrab + ".json"+"?auth="+token);
      urlRequest = (HttpURLConnection) url.openConnection();
      urlRequest.setDoOutput(true);
      urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
      urlRequest.connect();
      JsonParser jp = new JsonParser(); //from gson
      JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent())); //Convert the input stream to a json element
      JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 
      JSONObject myData = parseJSONObject(rootobj.toString());
      println("myData: " + myData);
    } 
    catch (Exception e) {
      println("getUserPosts Error: " + e);
    } 
    finally {
      urlRequest.disconnect();
    }
  }
}
1 Like