Get Resource URL Path in Java Spring -
trying path of json file have in resource folder failing getresource().getpath()
. url in robust manner possible.
public static void initializefirebase() { try { url url = getresource("google-services.json"); system.out.println(url.getpath()); firebaseoptions options = new firebaseoptions .builder() .setserviceaccount(new fileinputstream(url.getpath())) .setdatabaseurl("https://xxxxx.firebaseio.com/") .build(); firebaseapp.initializeapp(options); } catch (ioexception e) { e.printstacktrace(); } }
the above function prints: /users/username/documents/spring_project/springbackend/build/resources/main/google-services.json
before throwing error:
exception in thread "main" java.lang.runtimeexception: failed read service account @ com.google.firebase.firebaseoptions$builder.setserviceaccount(firebaseoptions.java:82) @ live.cibc.labs.springbackend.springbackendapplication.initializefirebase(springbackendapplication.java:188) @ live.cibc.labs.springbackend.springbackendapplication.main(springbackendapplication.java:61) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147) caused by: org.json.jsonexception: jsonobject["project_id"] not found.
obviously cannot find project_id, google-services.json under src/main/resources/google-services.json
.
how should fetch path robust manner possible?
google-services.json
{ "project_info": { "project_number": "xxxxxxxx", "firebase_url": "https://xxxxxx.firebaseio.com", "project_id": "xxxx-xxx", "storage_bucket": "xxxxxxxx.appspot.com" }, "client": [ { "client_info": { "mobilesdk_app_id": "1:xxxxxxxxx:android:xxxxxxx", "android_client_info": { "package_name": "my.spring.project" } }, "oauth_client": [ { "client_id": "xxxxx-xxxxx.apps.googleusercontent.com", "client_type": 3 } ], "api_key": [ { "current_key": "xxxxxx" } ], "services": { "analytics_service": { "status": 1 }, "appinvite_service": { "status": 1, "other_platform_oauth_client": [] }, "ads_service": { "status": 2 } } } ], "configuration_version": "1" }
the answer ended being stupid on part! in firebase console there 2 json files can download.
google-services.json
clientservice-account-credentials.json
server admin sdk (the 1 should have been using)
don't mix 2 up!
Comments
Post a Comment