java - Using private key (p12) embedded in a jar file for GoogleCredential Builder -
i using google sheets api query spreadsheet on google. code works fine when run inside eclipse. but, when project packed jar (say, spreadsheets-api.jar) , used different project maven dependency, running issues following method call:
public static sheets createservice() throws googleserviceinitexception { if (properties == null) init(); url keyfile = googlespreadsheetservice.class.getresource(getvalue(propertyenum.privatekey_file)); if (keyfile == null) throw new googleserviceinitexception("missing private key file"); credential credential; try { credential = new googlecredential.builder().settransport(http_transport).setjsonfactory(json_factory) .setserviceaccountid(getvalue(propertyenum.service_account_id)) .setserviceaccountprivatekeyfromp12file(paths.get(keyfile.touri()).tofile()) .setserviceaccountscopes(scopes).build(); return new sheets.builder(http_transport, json_factory, credential).setapplicationname(application_name) .build(); } catch (generalsecurityexception | ioexception | urisyntaxexception e) { logger.error(apgeneralutilities.stacktracetostring(e)); throw new googleserviceinitexception(e.getmessage()); } }
the issue method setserviceaccountprivatekeyfromp12file
takes java.io.file
parameter. so, when try access file referring embedded p12 file in jar url , coverting file using line of code - paths.get(keyfile.touri()).tofile()
, code bombs java.nio.file.filesystemnotfoundexception
.
the problem pretty clear. need refer p12 file stream, , using class.getresourceasstream() or other alternatives, google's credential builder take file parameter.
what options now? way see it, work sheets api, have stream, copy temp file, give file's path parameter credentials builder, , delete temp file after done. there simpler way solve problem?
Comments
Post a Comment