Java Google电子表格“令牌无效-AuthSub令牌的作用域错误”
我正试图授权对谷歌电子表格的请求。以下是我用来获取凭证的代码:Java Google电子表格“令牌无效-AuthSub令牌的作用域错误”,java,google-api,google-spreadsheet-api,Java,Google Api,Google Spreadsheet Api,我正试图授权对谷歌电子表格的请求。以下是我用来获取凭证的代码: public static Credential authorize() throws IOException, GeneralSecurityException { InputStream p12 = GoogleUtils.class.getResourceAsStream("/key.p12"); File file = new File("hi"); OutputStream
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.setOAuth2Credentialcredential
每当我运行这个时,我都会得到这个确切的错误
我几乎可以肯定,p12键没有任何问题,因为我在另一个项目中测试了完全相同的代码,所有目录/文件的东西都解决了。我试着弄到新的p12键,但没用。如果我遗漏了什么,我会很高兴听到的。谢谢。我一直使用错误的示波器:/
static{
service = new SpreadsheetService("Main");
try {
service.setOAuth2Credentials(GoogleUtils.authorize());
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}