0

I need my app to upload files, on the GDrive account, that can be listed and read by other user accounts (same app, other devices)

I am uploading files from aUserFileCreator@gmail.com and set the permission to anyone/reader + make it public with allowFileDiscovery

File file = driveService.files()
        .create(fileMetadata, mediaContent)
        .setFields("id")
        .setSupportsAllDrives(true)
        .setIgnoreDefaultVisibility(true)
        .execute();


Permission p = new Permission();
p.setType("anyone");
p.setRole("reader");
p.setAllowFileDiscovery(true);
driveService.permissions()
        .create(file.getId(), p)
        .execute();

Share the file later with TheUserToListAndRead@gmail.com

Permission accessPermission = new Permission();
accessPermission.setEmailAddress("TheUserToListAndRead@gmail.com");
accessPermission.setType("user");
accessPermission.setRole("reader");
driveService.permissions().create(fileId, accessPermission).execute();

When trying to list the files, like below, I am not getting anything back but the files are visible in the Drive app of TheUserToListAndRead@gmail.com

FileList result = driveService.files().list()
                    .setQ("not 'me' in owners")
                    .setIncludeItemsFromAllDrives(true)
                    .setSupportsAllDrives(true)
                    .setSpaces("drive,appDataFolder")
                    .setCorpora("allDrives")
                    .execute();


Alternatively I used setQ("sharedWithMe") with no success

The code works for files in the readers Drive account (only created and owned by the reader) when I remove setQ completely or set it to a mime-type of some sort

mekanix
  • 1
  • 2

1 Answers1

0

First let me say I am NOT an android developer, i do however know the google api java client library. I do not know if this works in andorid or not and would love to hear if it does.

What you are looking for is something called a service account. Service accounts are like dummy users. If you share a directory with a service account then the service account will have access to this directory without having to authorize a user to access it.

GoogleCredential credential = GoogleCredential.fromStream(new 
FileInputStream("MyProject-1234.json"))
.createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));

How to create service account credetials. just remember to enable the google drive api under library.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Actually the implementation an use of a service account was easy - got scared with some comments found. Now I tried 2 approaches: 1. - create the app folder from user1@gmail.com - create app folder permission type user role writer for the serviceAccount - upload the file from serviceAccount DriveInstance to the app folder (set parent to folder id from prev step) and *I get the error code 400: **You cannot share this item because it has been flagged as inappropriate** * – mekanix Dec 02 '22 at 06:00
  • And 2: - create the app folder from user1@gmail.com - create app folder permission type user role writer for the serviceAccount - upload the file from user1@gmail.com and set file permission type user , role writer to my serviceAccount (it is already inherited from the parent folder but ...) - share the file with userIWantToRead@gmail.com from user1@gmail.com app - read files from the app of user1@gmail.com, using serviceAccount DriveInstance and does not show anything – mekanix Dec 02 '22 at 06:03
  • for 1. https://stackoverflow.com/a/74103726/1841839 <--- try this anwser. – Linda Lawton - DaImTo Dec 02 '22 at 08:21
  • read files from the app of user1@gmail.com, using serviceAccount DriveInstance and does not show anything Well that definatly not going to work. You need to be authorized as user1@gmail.com to see user1's data, the service account can only list what it has access to. – Linda Lawton - DaImTo Dec 02 '22 at 08:24
  • but the service account has writer permission on the app folder and files generated and uploaded by the user1@gmail.com.... – mekanix Dec 02 '22 at 12:57
  • tried with other files types and managed to upload some with a service account instance of Drive class - it was displaying created by 'serviceAccountEmail@gmail.com'. Deleted everything to start from scratch and now I get the same error, on these types too 'flagged as inappropriate'. As far as I see I could upload files after a whi;e after encountered the error so, I assume, there is some sort of review of the files a service account is uploading, and as soon as you upload other types, uploads are not approved till the next review. Will try again tomorrow – mekanix Dec 03 '22 at 07:17
  • Think of a service account as a dummy user. When the service account uploads the file its uploaded by the service account it is then owner of the file. Granting other users access to the file will be needed unless your using a google workspace account and configure delegation. 'flagged as inappropriate' what type of file are you uploading? – Linda Lawton - DaImTo Dec 03 '22 at 09:50
  • The service account is actually another user, but virtual or headless - name it as you will, with an existing email so it is to be treated as such- needs permissions and everything that you would do for any other user – mekanix Dec 03 '22 at 17:45
  • I can confirm that, in my case, files uploaded with the service account appear in the user1@gmail.com after about 24 hours of being labeled as inappropriate - why it is like that would be interesting to know – mekanix Dec 04 '22 at 08:35
  • what the file type is that is being flagged as inappropriate would also be interesting – Linda Lawton - DaImTo Dec 04 '22 at 08:41
  • any file type is flagged this way and after 24 hours released. My app is not published yet, could it be a reason?! – mekanix Dec 05 '22 at 09:08
  • it could be but if it is its new I havent seen this before TBH you have me stumped. I have pinged someone at google for the fun of it. – Linda Lawton - DaImTo Dec 05 '22 at 09:26