javagoogle-apigoogle-drive-apigoogle-api-java-clientservice-accounts

Service account created folders do not apear in google drive web application


Not able to view folders created using google api in google drive. I am using service account json to connect to google drive. I can see the files getting created using api but cannot view the folders created using api."

Connecting to google drive using service account

             GoogleCredential cr = GoogleCredential
            .fromStream(new FileInputStream(CREDENTIALS_FILE_PATH))
            .createScoped(SCOPES);
             GoogleCredential builder = new GoogleCredential.Builder()
            .setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountId(cr.getServiceAccountId())
            `enter code here` 

            .setServiceAccountPrivateKey
            (cr.getServiceAccountPrivateKey())

            .setServiceAccountPrivateKeyId
             (cr.getServiceAccountPrivateKeyId())
            .setTokenServerEncodedUrl(cr.getTokenServerEncodedUrl())
            .setServiceAccountUser(cr.getServiceAccountUser()).build();

======================================================================

Creating a folder using below API

                File fileMetadata = new File();
                fileMetadata.setName("TestFolder");
                fileMetadata.setMimeType("application/vnd.google- 
                apps.folder");

                File file1 =
                service.files().create(fileMetadata).setFields("id, 
                name").execute();

I can see the folder ID getting created but when i login to google drive , there are no folders in the google drive


Solution

  • What you need to remember is that a Service account is not you. A service account is a dummy user. It has its own google drive account. when you upload the files to the service accounts drive account they are only accessible by that account as it is the owner of it.

    Share a drive

    You could create a drive on your google drive account share that directory with the service accounts email address. Then the service account can upload to that drive (parent) id. The files will then apear in that drive make sure that you have the service account grant your google account permissions on the file.

    Share the file

    If you want to leave the file on the Service account drive account then you can simply have the service account share the file with you then it will popup as a shared file in your google drive account.

    sharing

    Open the Google drive website right click the directory or file you want to share and click share. Take the service accounts email address and share it with that. It will then have access to that directory.

    enter image description here