我的应用程序(在glassfish上运行)访问谷歌电子表格以检索某些信息,我使用 google-api-client执行OAuth并获取对电子表格的访问权限。
有关OAuth和Spreadsheet API的更多详细信息,请访问: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
以下是获取AuthenticatedService
的代码片段 public SpreadsheetService getAuthenticatedService() {
SpreadsheetService service =
new SpreadsheetService("MySpreadsheetIntegration-v1");
try {
//Generating a token every time, for the current requirements this is fine.
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String filePath = RW_SW_P_12_KEY;
String keyFilePath = new File(this.getClass().getResource(filePath).toURI()).getAbsolutePath();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAccountEmail)
.setServiceAccountPrivateKeyFromP12File(new File(keyFilePath))
.setServiceAccountScopes(Collections.singleton("https://spreadsheets.google.com/feeds"))
.setServiceAccountUser(userName)
.setClientSecrets(clientId, clientSecret)
.build();
credential.setExpiresInSeconds(3600L);
service.setOAuth2Credentials(credential);
service.setProtocolVersion(SpreadsheetService.Versions.V3);
} catch (IOException ioe) {
LOGGER.error(ioe.getMessage(), ioe);
Throwables.propagate(ioe);
} catch (GeneralSecurityException se) {
LOGGER.error(se.getMessage(), se);
Throwables.propagate(se);
} catch (URISyntaxException use) {
LOGGER.error(use.getMessage(), use);
Throwables.propagate(use);
}
return service;
}
但是我遇到了以下错误:
exception unwrapping private key - java.security.InvalidKeyException: Illegal key size
java.io.IOException: exception unwrapping private key - java.security.InvalidKeyException: Illegal key size
at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.unwrapKey(Unknown Source)
at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.engineLoad(Unknown Source)
at java.security.KeyStore.load(KeyStore.java:1185)
at com.google.api.client.util.SecurityUtils.loadKeyStore(SecurityUtils.java:82)
at com.google.api.client.util.SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.java:115)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder.setServiceAccountPrivateKeyFromP12File(GoogleCredential.java:670)
at com.bwc.stratus.steelwedge.GSpreadSheetServiceImpl.getAuthenticatedService(GSpreadSheetServiceImpl.java:89)
我找到了一个有效的工作
http://opensourceforgeeks.blogspot.com/2014/09/how-to-install-java-cryptography.html
然而,当我试图回到旧的"限制"策略文件(local_policy.jar,US_export_policy.jar),我没有再收到任何错误。这对我来说没有意义,任何有助于解决这个谜团的解释都是值得赞赏的。