Home > Back-end >  Connection Reset error with Google Drive APIs
Connection Reset error with Google Drive APIs

Time:02-13

I am using a Google Service account to create Google Drive resources.

However when i try to execute Drive create folder API i get connection reset error.

I can confirm that the credentials for the service account are properly configured and loaded in my program.

Below is the code and the stacktrace

 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;
}

Below is how the credentials are read and instance of Drive is created

/**
 * 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);

Stacktrace

    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$2.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$1.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)

CodePudding user response:

you are using DriveScopes.DRIVE_FILE this scope can act a bit strange sometimes. Its basicly used to lock an application and only allow it to edit files that it created itself. When creating folders I have found it can act very strangely if the directory its creating is not inside a directory it created in the first place.

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

The solution is to just use. DriveScopes.DRIVE technically you are using a service account anyway there is really no reason to limit it.

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

  • Related