我正在多个数据节点上运行Hadoop作业。为此我提供了一个包含我的工作的.jar
文件。
在我想要访问Google AnalyticsAPI的工作中(参见introduction tutorial)。对于身份验证,Analytics API需要.p12
文件进行身份验证。基本上我有this problem。使用 gradle 我可以将此文件打包到我的jar
文件中:
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
,该文件位于jar
的根目录中。现在我尝试修改示例代码以从jar 或中读取.p12
文件以任何其他方式访问它。
我尝试了多种方式,但总是以NullPointerException
结束。这是我最近的尝试:
File file = new File(HelloAnalytics.class.getResource("client_secrets.p12").toURI());
HttpTransport httpTransport = GoogleNetHttpTransport
.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(file)
.setServiceAccountScopes(AnalyticsReportingScopes.all())
.build();
我也尝试用BufferedReader
读取文件并将其导出回文件系统:
File file = null;
String resource = "client_secrets.p12";
URL res = getClass().getResource(resource);
if (res.toString().startsWith("jar:")) {
InputStream input = getClass().getResourceAsStream(resource);
file = File.createTempFile("client_secrets", ".p12");
OutputStream out = new FileOutputStream(file);
int read;
byte[] bytes = new byte[1024];
while ((read = input.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
file.deleteOnExit();
} else {
//this will probably work in your IDE, but not from a JAR
file = new File(res.getFile());
}
if (file != null && !file.exists()) {
throw new IOException("...");
}
HttpTransport httpTransport = GoogleNetHttpTransport
.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(file)
.setServiceAccountScopes(AnalyticsReportingScopes.all())
.build();
有关如何正确访问jar
的任何想法?