Google Drive API 的连接重置错误

Connection Reset error with Google Drive APIs

我正在使用 Google 服务帐户来创建 Google 驱动器资源。

然而,当我尝试执行 Drive create folder API 时,出现连接重置错误。

我可以确认服务帐户的凭据已正确配置并加载到我的程序中。

下面是代码和堆栈跟踪

 public static File createFolder(Drive driveService) {
    File fileMetadata = new File();
    fileMetadata.setName("Testing");
    fileMetadata.setMimeType("application/vnd.google-apps.folder");

    File file =null;
    try {
        file = driveService.files().create(fileMetadata)
            .setFields("id")
            .execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Exception here in create folder");
        e.printStackTrace();
    }
    System.out.println("Folder ID: " + file.getId());
    
    return file;
}

下面是读取凭据和创建 Drive 实例的方式

/**
 * Creates credentials for the service account
 * @param HTTP_TRANSPORT
 * @return
 * @throws IOException
 */
private static Credential getServiceAccountCredential(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
     InputStream in = DriveQuickStart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
     if (in == null) {
         throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
     }
     GoogleCredential credential = GoogleCredential.fromStream(in)
             .createScoped(SCOPES);
     System.out.println("loaded credential " + credential.getServiceAccountId()) ;
     return credential;
}

public static void main(String... args) throws IOException, GeneralSecurityException {
    // Build a new authorized API client service.
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    SCOPES.add(DriveScopes.DRIVE_FILE);
    Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getServiceAccountCredential(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();
    //create folder
    
   File parentFolder =  createFolder(service);

堆栈跟踪

    Exception here in create folder
javax.net.ssl.SSLException: Connection reset
        at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:369)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:312)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
        at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:144)
        at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1488)
        at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1394)
        at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:441)
        at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:412)
        at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567)
        at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1375)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1350)
        at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:220)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
        at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
        at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
        at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:394)
        at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
        at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at DriveQuickStart.createFolder(DriveQuickStart.java:138)
        at DriveQuickStart.main(DriveQuickStart.java:103)
        Suppressed: java.net.SocketException: Broken pipe
                at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:420)
                at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:440)
                at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:826)
                at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1051)
                at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:82)
                at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:400)
                ... 25 more
Caused by: java.net.SocketException: Connection reset
        at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
        at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
        at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:803)
        at java.base/java.net.Socket$SocketInputStream.read(Socket.java:981)
        at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:478)
        at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:472)
        at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:160)
        at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:110)
        ... 22 more
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.google.api.services.drive.model.File.getId()" because "file" is null
        at DriveQuickStart.createFolder(DriveQuickStart.java:144)
        at DriveQuickStart.main(DriveQuickStart.java:103)

您正在使用 DriveScopes.DRIVE_FILE 这个范围有时会有点奇怪。它基本上用于锁定应用程序,只允许它编辑自己创建的文件。在创建文件夹时,我发现如果它创建的目录不在它最初创建的目录中,它会表现得很奇怪。

https://www.googleapis.com/auth/drive See, edit, create, and delete all of your Google Drive files

解决办法就是直接使用。 DriveScopes.DRIVE 从技术上讲,无论如何您都在使用服务帐户,真的没有理由限制它。

https://www.googleapis.com/auth/drive.file View and manage Google Drive files and folders that you have opened or created with this app