【发布时间】:2015-09-12 23:54:10
【问题描述】:
我正在尝试授权对 Google 电子表格提出的请求。这是我用来获取凭证的代码:
public static Credential authorize() throws IOException, GeneralSecurityException {
InputStream p12 = GoogleUtils.class.getResourceAsStream("/key.p12");
File file = new File("hi");
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(p12, outputStream);
outputStream.close();
// Build flow and trigger user authorization request.
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("8429209348-1nfuorcfko5pmqh2l0b1au968igchaoq@developer.gserviceaccount.com")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(file)
.build();
return credential;
}
这是另一个类中的代码,它获取凭证并调用SpreadsheetService.setOAuth2Credential(credential);
static{
service = new SpreadsheetService("Main");
try {
service.setOAuth2Credentials(GoogleUtils.authorize());
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
每当我运行它时,我都会得到这个确切的错误。
我几乎可以肯定 p12 键没有任何问题,因为我已经在另一个项目中测试了完全相同的代码,并且所有目录/文件内容都可以正常工作。我尝试获取新的 p12 密钥,但无济于事。如果有什么我想念的,我会很高兴听到的。谢谢。
【问题讨论】:
标签: java google-api google-spreadsheet-api