Aviv Abramovich
Aviv Abramovich

Reputation: 135

Access denied to file in my Google App Engine project

I'm developing an application backend using Google App Engine in Android Studio. I want to use Firebase so part of it's SDK initialization is read a Service account Credentials JSON file. I added the file to my "src" directory and try to read it like in the Firebase's doc:

FirebaseOptions options = new FirebaseOptions.Builder()
    .setServiceAccount(new FileInputStream("/src/serviceAccountCredentials.json"))
    .setDatabaseUrl("https://databaseName.firebaseio.com/")
    .build();

But it always throw exception that access to the file is denied:

java.security.AccessControlException: 
    access denied ("java.io.FilePermission" "/src/serviceAccountCredentials.json" "read")

How can I change the read permission to this file? Thanks :)

Upvotes: 1

Views: 722

Answers (2)

Manish Patiyal
Manish Patiyal

Reputation: 4467

You need to add the file in your appengine-web.xml file under the tag.

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>myApplicationId</application>
    <version>1</version>
    <threadsafe>true</threadsafe>
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
    </system-properties>
    <resource-files>
        <include path="secret.json" />
    </resource-files>
</appengine-web-app>

This way your app engine server will come to know that you are using this static resource which is read only.

Upvotes: 0

Igor Artamonov
Igor Artamonov

Reputation: 35961

Your is located in the source dir, so after compilation it should be at root of classpatch. But make sure it's there, it depend on build tools actually, some of them can ignore non source file in src dir, because such files should be in resources dir. If that your case, the move file to resources dir.

Then, to read this file, you have to use it as a resource:

InputStream cred = this.getClass()
          .getResourceAsStream("/serviceAccountCredentials.json");

FirebaseOptions options = new FirebaseOptions.Builder()
         .setServiceAccount(cred)
         .setDatabaseUrl("https://databaseName.firebaseio.com/")
         .build();

Upvotes: 1

Related Questions