Firebase Service Account Accounts Json Read Disclaimer

I downloaded the json account of the service account file from the Firebase console, placed earlier in the main directory of the GAE endpoint project, when I run locally locally, it provides a security exception.

java.security.AccessControlException: access denied ("java.io.FilePermission" "\src\main\secret.json" "read") 

I tried to place the .json file in the src directory, but without help.

+5
source share
3 answers

Finally, I found a solution written in the Google App Engine API and Links section in this link that we need to add such files in the appengine-web.xml file under the <resource-files> using the <include path=""/> property . After that his work is for me. I placed the .json file containing the project credentials in the WEB-INF directory, and then entered its relative path in the <resource-files> .

0
source

You should put the json file in src/main/resources

0
source

I found a couple of ways to approach this. First, get it from a file through an internet stream. The other is locally.

INTERNET WAY

My first method involved storing the file in my public Dropbox folder. I got a generic link (make sure it ends with .json ) and pasted it into the example line "https://dl.dropboxusercontent.com/..EXAMPLE-CREDENTIALS"

 /** A simple endpoint method that takes a name and says Hi back */ @ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { MyBean mModelClassObject = null; String text = ""; try { String line = ""; StringBuilder builder = new StringBuilder(); URL url = new URL("https://dl.dropboxusercontent.com/..EXAMPLE-CREDENTIALS"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = reader.readLine()) != null) { // ... builder.append(line); } reader.close(); text = builder.toString(); } catch (MalformedURLException e) { // ... } catch (IOException e) { // ... } InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); FirebaseOptions options = null; options = new FirebaseOptions.Builder() .setServiceAccount(stream) .setDatabaseUrl("https://[PROJECT-ID].firebaseio.com/") .build(); FirebaseApp.initializeApp(options); DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); final TaskCompletionSource<MyBean> tcs = new TaskCompletionSource<>(); Task<MyBean> tcsTask = tcs.getTask(); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { MyBean result = dataSnapshot.getValue(MyBean.class); if(result != null){ tcs.setResult(result); } } @Override public void onCancelled(DatabaseError databaseError){ //handle error } }); try { mModelClassObject = Tasks.await(tcsTask); }catch(ExecutionException e){ //handle exception }catch (InterruptedException e){ //handle exception } return mModelClassObject; } 

LOCAL WAY

Another way is to take the version above and skip something like dropbox

 /** A simple endpoint method that takes a name and says Hi back */ @ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { MyBean mModelClassObject = null; String text = "JUST PASTE YOUR JSON CONTENTS HERE"; InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); FirebaseOptions options = null; options = new FirebaseOptions.Builder() .setServiceAccount(stream) .setDatabaseUrl("https://[PROJECT-ID].firebaseio.com/") .build(); FirebaseApp.initializeApp(options); DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); final TaskCompletionSource<MyBean> tcs = new TaskCompletionSource<>(); Task<MyBean> tcsTask = tcs.getTask(); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { MyBean result = dataSnapshot.getValue(MyBean.class); if(result != null){ tcs.setResult(result); } } @Override public void onCancelled(DatabaseError databaseError){ //handle error } }); try { mModelClassObject = Tasks.await(tcsTask); }catch(ExecutionException e){ //handle exception }catch (InterruptedException e){ //handle exception } return mModelClassObject; } 

I do not know if this is in line with best practice, but my project is working now. I also included firebase code for information. look at this answer to a question I recently asked about reading and writing in firebase.

EDIT

cleaned version that does not throw errors

 public class MyEndpoint { private FirebaseOptions options; private DatabaseReference ref; private String serviceAccountJSON = "i took mine out for security reasons"; // create firebase instance if need be private void connectToFirebase(){ if (options == null) { options = null; options = new FirebaseOptions.Builder() .setServiceAccount(new ByteArrayInputStream(serviceAccountJSON.getBytes(StandardCharsets.UTF_8))) .setDatabaseUrl("https://[PROJECT-ID].firebaseio.com/") .build(); FirebaseApp.initializeApp(options); } if(ref == null) { ref = FirebaseDatabase.getInstance().getReference(); } } /** A simple endpoint method that takes a name and says Hi back */ @ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { // always do this first connectToFirebase(); MyBean mModelClassObject = null; final TaskCompletionSource<MyBean> tcs = new TaskCompletionSource<>(); Task<MyBean> tcsTask = tcs.getTask(); // get the info ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { MyBean result = dataSnapshot.getValue(MyBean.class); if(result != null){ tcs.setResult(result); } } @Override public void onCancelled(DatabaseError databaseError){ //handle error } }); // wait for it try { mModelClassObject = Tasks.await(tcsTask); }catch(ExecutionException e){ //handle exception }catch (InterruptedException e){ //handle exception } mModelClassObject.setData(mModelClassObject.getData() + name); return mModelClassObject; } } 
0
source

All Articles