尝试GoogleNetHTTPTransport时“未找到JKS”

Jua*_*via 7 android credentials keystore jks google-oauth2

我在使用Google授权方面遇到了一些麻烦,而且之前从未使用过任何“涉及Google凭证的”流程。

我的问题发生在创建凭证读取器之后(我认为这意味着我可以正确访问Google凭证的JSON文件),就在从中实例化新的Trusted Transport的行中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)
Run Code Online (Sandbox Code Playgroud)

我一直在调查,但是仍然找不到关于这些运输工具如何工作或为什么KeyStore参与此过程的正当而清晰的解释。

我的代码是AsyncTask我用来从Google表格中获取数据的一部分,我目前正试图对其进行访问。在这里,我留下了您的authorize()方法,该方法将检索我并将Credential其传递给我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;
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您,我才刚刚开始Google的开发,我对了解其所有可能性非常感兴趣。我也对编码提出任何建议。

小智 15

这是帮助我解决此问题的答案 /sf/answers/2747497861/ 您必须替换

HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Run Code Online (Sandbox Code Playgroud)

HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport()
Run Code Online (Sandbox Code Playgroud)