4

I am using java google drive api to upload files. My problem is after uploading files i did not find uploaded files from my google drive. How can i see that files in web browser. Any idea for that please help me?

My java code is :

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

    try {
        GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(IRingeeConstants.SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE)).setServiceAccountPrivateKeyFromP12File(new java.io.File("G:\\Ringee-1a1f1b786226.p12")).build();

        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName(IRingeeConstants.APPLICATION_NAME).build();

        printAbout(service);

        File body = new File();
        body.setTitle("ringeeprofileimage");
        body.setDescription("ringeeapp");
        body.setMimeType("application/vnd.google-apps.folder");

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

        File file = service.files().insert(body, mediaContent).execute();
        System.out.print("file id is :" + file.getId());
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
nmkkannan
  • 1,261
  • 4
  • 27
  • 49

2 Answers2

4

You are uploading files using a Service account. A service account is not you. Think of a service account as a dummy user identified by the service account email address.

A service account his its own Drive Account, Google Calendar, .....

So the files are uploaded they are just uploaded to the Service accounts drive account which you can not access using the web interface.

If you want to be able to see them, you will need to give the service account access to a directory on your Google Drive account, have it uploaded to that directory then patch the permissions after the insert to allow your personal account to access the files. This must be done in two steps, its a weird bug.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
1

Add this below code :

Permission newPermission = new Permission();
        //for showing files in browser that reason only using additional permission 
        newPermission.setValue(IRingeeConstants.USER_ACCOUNT_EMAIL);
        newPermission.setType("user");
        newPermission.setRole("writer");
        service.permissions().insert(file.getId(), newPermission).execute();

User account mail is nothing but service email id like nmkkannan@gmail.com

Its solved..

nmkkannan
  • 1,261
  • 4
  • 27
  • 49