与此类似的问题已发布,但似乎没有一个答案对我的情况有帮助。
我正在编写一个程序包,它使用 Google 的凭据来获取 Google Apps 用户。为此,我使用服务帐户,因此为了检索凭据,我需要提供(除其他外)一个 p12 签名文件:
Credential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountEmail)
.setServiceAccountScopes(SCOPES)
.setServiceAccountUser(serviceAccountUser)
.setServiceAccountPrivateKeyFromP12File( java.io.File ))
最后一个函数必须接收 p12 签名文件的 java.io.File 对象。现在,整个事情都在我提供给其他人的罐子里运行,这造成了大部分问题。无论我采取哪种方法,在 jar 内运行时我都无法读取该文件。我尝试过的事情包括:
return new File(GoogleUserFactory.class.getClassLoader().getResource("/" + filePath).toURI());
// (我也尝试过不使用“/”并直接使用 class.GetResource() )
URL url = GoogleUserFactory.class.getResource("/" + filePath);
return new File(url.getPath());
// 我也尝试过 class.GetClassLoader.GetResource()...
甚至尝试将文件作为InputStreamReader读取并写入一个新文件(在我将为其创建一个文件对象并返回它之后),但由于我在一个jar中,我似乎没有权限写入新文件)
也许你可以使用.setServiceAccountPrivateKey(SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), SOME_INPUT_STREAM, "notasecret", "privatekey", "notasecret");
,如提议的here https://code.google.com/p/google-api-java-client/issues/detail?id=857 ?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)