1

I have to authorize my server to Firebase for the Firebase SDK. But unfortunately I can't read the credentials.json file. I have put my service.json file into my WEB-INF folder.

I have added this to my appengine-web.xml file:

<resource-files>
    <include path="/service.json"/>
</resource-files>

And I am trying to read the file with this code:

FirebaseOptions options = new FirebaseOptions.Builder().setServiceAccount(ServletServletContext.class.getResourceAsStream("/WEB-INF/service.json"))

But when I try to read the file I get a NullPointerException.

This is my whole class:

@Api(
  name = "myApi",
version = "v1",
namespace = @ApiNamespace(
ownerDomain = "backend",
ownerName = "backend",
packagePath=""
  )
)
public class MyEndpoint {

private static final Logger log = Logger.getLogger(MyEndpoint.class.getName());
private String uid;

@ApiMethod(name = "signup")
public MyBean signup(@Named("token")String token)
{
    uid = "empty";
    uid = "init";

    log.info(new File(getClass().getResource("/WEB-INF/service.json").toString()).exists()+"");

    FirebaseOptions options = new FirebaseOptions.Builder()
                .setServiceAccount(getClass().getResourceAsStream("/WEB-INF/service.json"))
                .setDatabaseUrl("url")
                .build();

    FirebaseApp.initializeApp(options);
    uid = "initialized";

    log.severe("initialized");
    FirebaseAuth.getInstance().verifyIdToken(token)
            .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {

                    @Override
                    public void onSuccess(FirebaseToken decodedToken) {uid = decodedToken.getUid();
                    }
            });

    MyBean b = new MyBean();
    if(!uid.equals(""))
        b.setData(uid);
    // else
    // b.setData("failed");

    return b;
}

}

Thanks in advance for your help.

ReasoN
  • 366
  • 4
  • 17
  • There is no need to modify appengine-web.xml. How do you read the file? – Andrei Volgin Jul 23 '16 at 13:25
  • When I am using a FileInputStream to read this file I am getting a access denied, but then I have looked it up and someone said I should do it like this, but with this code I am just getting NullPointerExceptions. – ReasoN Jul 23 '16 at 13:31
  • There are many questions on StackOverflow that explain how to read a static file on App Engine. For example: http://stackoverflow.com/questions/1362545/how-to-load-properties-file-in-google-app-engine?rq=1 – Andrei Volgin Jul 23 '16 at 13:42
  • 1
    I have tried the answer, but it is still not working. Where do I have to put the file? Currently I got it in my WEB-INF folder and what is the path to this file? – ReasoN Jul 23 '16 at 13:54
  • See http://stackoverflow.com/questions/4340653/file-path-to-resource-in-our-war-web-inf-folder – Andrei Volgin Jul 23 '16 at 16:48
  • I don't have the method getContext() in my class. I want to do that in a Endpoint class if that is important. – ReasoN Jul 23 '16 at 16:59

1 Answers1

2

You can use

this.getClass().getResourceAsStream("/WEB-INF/service.json");
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • 1
    I am still getting NullPointerExceptions with this solutions – ReasoN Jul 23 '16 at 20:32
  • NullPointerException should point to specific line of code. Which one? – Andrei Volgin Jul 23 '16 at 20:42
  • setServiceAccount(this.getClass().getResourceAsStream("/WEB-INF/service.json")) this one – ReasoN Jul 23 '16 at 20:44
  • Are you using it in the right context? What class contains this method? – Andrei Volgin Jul 23 '16 at 23:53
  • I have added my whole class to my question if this helps. – ReasoN Jul 24 '16 at 11:25
  • You log statement with `new File` does not make sense. – Andrei Volgin Jul 24 '16 at 12:40
  • I just tried something out and didn't delete it yet. – ReasoN Jul 24 '16 at 12:51
  • Ok I have tried something to create the inputstream with a string and now it says I can only use Firebase on a backend. So my question is, can I use firebase with Endpoints? – ReasoN Jul 24 '16 at 20:14
  • Your `/WEB-INF/` is on your backend. And yes, you can only use it on the backend. There is no need to call this method within a bean. – Andrei Volgin Jul 24 '16 at 21:31
  • So if I want to develop my backend with google app engine I have to call the endpoint with my app and the endpoint is communicating with a servlet or am I misunderstanding something? – ReasoN Jul 25 '16 at 17:10
  • Your endpoint is your backend/servlet. You just need to restructure your code, where no server-side code is included in entities that are shared with the client. – Andrei Volgin Jul 25 '16 at 17:18
  • Where do I have to put the code, because I put it in a private method, which I am calling in the apimethod, but it is still not working. Could you tell me where I have to put the code? – ReasoN Jul 25 '16 at 17:43
  • Ok I have found the problem. I had to add manual-scaling to my xml file. – ReasoN Jul 25 '16 at 19:38
  • @ReasoN How is manual-scaling related to including *.json* files? may you provide details on the code for manual scaling and where you implemented it. – AdamHurwitz Oct 21 '18 at 22:10
  • @ReasoN I don't think manual scaling is related as I attempted to add the stream path solution and ran it locally in my IDE and got the NPE. – AdamHurwitz Oct 21 '18 at 22:18
  • I don’t know. It’s just that app engine didn’t allow that at the time I tried doing it. Maybe they changed it, but I don’t know. – ReasoN Oct 21 '18 at 22:19