Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Admin SDK with Heroku

I am attempting to initialize the Firebase Admin SDK using a Heroku instance running a Java Spring application. The Firebase instructions reference the .json file directly.

Initialize the Firebase Admin SDK

I do not want to include my admin SDK json file directly in my git repo because I don't want my firebase credentials exposed. To access the .json values in Heroku I have created an environment variable with the contents of the Firebase Admin SDK .json file inside of it. I have done the same thing in my local development environment and the app works by accessing the environment var using this method:

String serviceAccountJson = System.getenv("SERVICE_ACCOUNT_JSON");
InputStream serviceAccount = new ByteArrayInputStream(serviceAccountJson.getBytes(StandardCharsets.UTF_8));
FirebaseOptions options = new FirebaseOptions.Builder()
    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))'
    .setDatabaseUrl("https://myfirebase-app.firebaseio.com/")
    .build();
FirebaseApp.initializeApp(options);

When I attempt to run the app in Heroku it crashes when firebase tries to initialize.

Are there any work arounds or other methods to access a .json file that is not in a .git repo, but available on a Heroku server?

like image 919
adam2k Avatar asked May 30 '17 17:05

adam2k


Video Answer


1 Answers

I was running into the same issue. You just need to escape any special characters in the json with something like this.

like image 118
adrice727 Avatar answered Sep 27 '22 15:09

adrice727