我在Google授权方面遇到了一些麻烦,之前我从未使用任何“Google凭据参与”流程。我的问题发生在我创建凭据读取器之后(我假设这意味着我可以正确访问我的Google凭证的json文件),就在我从GoogleNetHTTPTransport实例化新的可信转移的行中。在那里,异常错误抛出:
W/System.err: java.security.KeyStoreException: JKS not found
at java.security.KeyStore.getInstance(KeyStore.java:649)
at com.google.api.client.util.SecurityUtils.getJavaKeyStore(SecurityUtils.java:53)
at com.google.api.client.googleapis.GoogleUtils.getCertificateTrustStore(GoogleUtils.java:74)
at com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(GoogleNetHttpTransport.java:55)
at com.example.juans.hasapp.getDataFromSheet.authorize(getDataFromSheet.java:70)
我一直在调查,但仍然无法找到关于这些传输如何工作的正确和明确的解释,或者为什么KeyStore参与了这个过程。
我的代码是我用于从Google表格中获取数据的AsyncTask的一部分,我目前正在尝试访问该表格。在这里,我留下 authorize()方法,该方法将检索我的凭证以传递给我的SheetsService。
private Credential authorize(Context context) {
Resources resources = context.getResources();
InputStream jsonInput = resources.openRawResource(R.raw.credential_info);
Reader credentialReader = null;
try {
credentialReader = new InputStreamReader(jsonInput);
Log.v("GoogleAut", "credential reader created");
} catch (NullPointerException n){
Log.v("ERROR GoogleAut", "filePath came out empty, no info provided");
}
GoogleClientSecrets clientSecrets = null;
try {
clientSecrets = GoogleClientSecrets
.load(JacksonFactory.getDefaultInstance(),credentialReader);
} catch (IOException e) {
e.printStackTrace();
Log.v("ERROR GoogleAut","IOException error occurred");
}
List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
GoogleAuthorizationCodeFlow flow = null;
try {
flow = new GoogleAuthorizationCodeFlow
.Builder(GoogleNetHttpTransport.newTrustedTransport()
, JacksonFactory.getDefaultInstance()
,clientSecrets
,scopes)
.setDataStoreFactory(new MemoryDataStoreFactory())
.setAccessType("offline")
.build();
} catch (IOException e) {
e.printStackTrace();
Log.v("ERROR GoogleAut","IOException error occurred");
} catch (GeneralSecurityException e) {
e.printStackTrace();
Log.v("ERROR GoogleAut","GeneralSecurityException error occurred");
}
try {
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize("user");
} catch (Exception e) {
e.printStackTrace();
Log.v("ERROR GoogleAut", "Unidentified error");
}
return null;
}
在此先感谢,我刚开始与谷歌开发,我对学习它的所有可能性非常感兴趣。我也对我的编码提出了任何建议。
答案 0 :(得分:7)
以下是帮助我解决此问题的答案 https://stackoverflow.com/a/39249969/8853293 您必须替换
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
使用
HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport()