我正在尝试授权向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键,但无济于事。如果有什么我想念,我会很高兴听到它。感谢。
答案 0 :(得分:0)
我的整个时间范围都是错误的:/