使用Java API对Google Drive进行身份验证时出现异常

使用Java API对Google Drive进行身份验证时出现异常,java,google-drive-api,google-api-java-client,Java,Google Drive Api,Google Api Java Client,我在用Google Drive API上传.txt文件时遇到一些问题。这是我的密码: import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.auth.oauth2.Goog

我在用Google Drive API上传.txt文件时遇到一些问题。这是我的密码:

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


public class DriveCommandLine {

    private static String CLIENT_ID = "client_id";
      private static String CLIENT_SECRET = "client_secret";

      private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

      public static void main(String[] args) throws IOException {

        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
            .setAccessType("online")
            .setApprovalPrompt("auto").build();

        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
        System.out.println("Please open the following URL in your browser then type the authorization code:");
        System.out.println("  " + url);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

        //Create a new authorized API client
        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

        //Insert a file  
        File body = new File();
        body.setTitle("Document Example");
        body.setDescription("This is a test of Google Drive SDK");
        body.setMimeType("text/plain");

        java.io.File fileContent = new java.io.File("document.txt");
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());
      }

}
问题出在这一行:

GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
错误是:

Exception in thread "main" javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1836)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1794)
    at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1777)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1296)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1273)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:523)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1087)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:79)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:299)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:147)
    **at DriveCommandLine.main(DriveCommandLine.java:43)**
Caused by: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:90)
    at sun.security.validator.Validator.getInstance(Validator.java:179)
    at sun.security.ssl.X509TrustManagerImpl.getValidator(X509TrustManagerImpl.java:314)
    at sun.security.ssl.X509TrustManagerImpl.checkTrustedInit(X509TrustManagerImpl.java:173)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:186)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1319)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:154)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:966)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1262)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1289)
    ... 10 more
Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:200)
    at java.security.cert.PKIXParameters.<init>(PKIXParameters.java:120)
    at java.security.cert.PKIXBuilderParameters.<init>(PKIXBuilderParameters.java:104)
    at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:88)
    ... 22 more
线程“main”javax.net.ssl.SSLException中的异常:java.lang.RuntimeException:意外错误:java.security.invalidalgorithParameterException:trustAnchors参数必须为非空 位于sun.security.ssl.Alerts.getSSLException(Alerts.java:208) 位于sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1836) 位于sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1794) 位于sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1777) 位于sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1296) 位于sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1273) 位于sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:523) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1087) 位于sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250) 位于com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:79) 位于com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895) 位于com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:299) 在com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:147) **位于DriveCommandLine.main(DriveCommandLine.java:43)** 原因:java.lang.RuntimeException:意外错误:java.security.InvalidalgorithParameterException:trustAnchors参数必须为非空 位于sun.security.validator.PKIXValidator.(PKIXValidator.java:90) 位于sun.security.validator.validator.getInstance(validator.java:179) 位于sun.security.ssl.X509TrustManagerImpl.getValidator(X509TrustManagerImpl.java:314) 在sun.security.ssl.X509TrustManagerImpl.CheckTrustedIt(X509TrustManagerImpl.java:173)上 位于sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:186) 位于sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126) 位于sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1319) 位于sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:154) 位于sun.security.ssl.Handshaker.processLoop(Handshaker.java:868) 位于sun.security.ssl.Handshaker.process_记录(Handshaker.java:804) 位于sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:966) 位于sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1262) 位于sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1289) ... 10多 原因:java.security.invalidalgorithParameterException:trustAnchors参数必须为非空 位于java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:200) 位于java.security.cert.PKIXParameters。(PKIXParameters.java:120) 位于java.security.cert.PKIXBuilderParameters。(PKIXBuilderParameters.java:104) 位于sun.security.validator.PKIXValidator.(PKIXValidator.java:88) ... 还有22个
您不能将此示例(即基于本地主机(客户端)的Google应用程序)与为WebApplication提供的方法一起使用。您需要实例化一个googleweb应用程序(服务器端使用HttpServlet),并使用doGet方法的重定向URI自动捕获代码并交换数据以获得正确的授权。请参阅和

我对google drive api不太熟悉,但它可能会抱怨与truststore和证书相关的内容。可能会再次检查您是否已使用适当的证书创建了信任库?也来看看