0

所以我正在尝试编写一个 Stash 帖子接收挂钩插件,最终将一些相关信息写入 Google 电子表格。为此,我必须从 .p12 文件中提取用于 Oauth2 身份验证的私钥。我做了几个测试项目,它们只是普通的 Java 命令行应用程序,我会从根目录中取出 .p12 文件并从那里调用它。我又做了一个,我会从资源文件夹中调用它,它也工作得很好。

我移植了该代码,整理好所有依赖项,然后运行它。我天真地将 .p12 文件放在根目录中,显然得到了一个找不到文件的异常。查看目标目录,发现它从来没有带来过,重新调整并将其放在资源文件夹中,看看是否会得到延续。另一个文件未找到异常并且仍然​​不在目标目录中。我试着把它放在其他几个不同的地方,看看它是否会被带过来。到目前为止没有任何工作。

我在这里做错了什么?

我想我可以将文件放在我的 Stash 实例上并从那里调用它,但我宁愿将它自包含在插件中。

我搜索了很多,但没有找到任何真正相关的东西。有人有什么建议吗?

4

1 回答 1

0

如果其他人看到这个并想知道我是如何解决它的... Atlassian 开发人员指出我的主要问题是我只是将 .p12 文件放在根资源目录中。一旦我把它放到那里自己的文件夹中,它就像一个魅力。我让这一切正常工作的最终代码(在其他 Stack Overflow 问题的帮助下)

String sheetURL = "XXXXXX@XXXXXX-XXXX.iam.gserviceaccount.com";
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};

final List SCOPES = Arrays.asList(SCOPESArray);
GoogleCredential credential = null;
InputStream stream = null;
File tempFile = null;

try {
    stream = this.getClass().getClassLoader().getResourceAsStream("authentication/key.p12");
    tempFile = File.createTempFile("temp", "temp");
    IOUtils.copy(stream, new FileOutputStream(tempFile));
}
catch (IOException e) {
    print(e.toString());
}
finally {
    if (stream != null) {
        IOUtils.closeQuietly(stream);
    }
}

credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId(sheetURL)
    .setServiceAccountScopes(SCOPES)
    .setServiceAccountPrivateKeyFromP12File(tempFile) 
    .build();
于 2016-02-18T16:48:39.393 回答