2

I'm uploading a file to Google Drive using a Google Service Account. File is getting uploaded successfully. But I need to share it with some stakeholders. So, while adding permissions to it, i'm getting this error: "message": "Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."

My code snippets are:

// create and return credential
private static Credential getCredentials2() throws IOException {
    
    java.io.File serviceAccountCredsFile = new java.io.File("creds.json");
    @SuppressWarnings("deprecation")
    GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(serviceAccountCredsFile))
            .createScoped(SCOPES);
    
    return credential;
}

// build and return an authorized drive client service
public static List<File> getDriveService() throws Exception {
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

    // Instantiating a client
    Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials2())
                .setApplicationName(APPLICATION_NAME)
                .build();
    
    System.out.println("Successfully created the Drive service!!!!!!!!!!!!!!!!!!!!");

    
    /***************************Uploading file on Google Drive Start*********************************/
    File fileMetadata = new File();
    fileMetadata.setName("1stFile.csv");
    fileMetadata.setParents(Collections.singletonList("10Tt3rfkaeHr1JOUQYRPTfqQQ2jdDJcy")); 
    fileMetadata.setDescription("Testing upload");
    fileMetadata.setMimeType("application/csv");        
    
    java.io.File fileTobeUploaded = new java.io.File("custid.csv");
    
    FileInputStream inputStream = new FileInputStream(fileTobeUploaded);
    InputStreamContent mediaContent = new InputStreamContent("application/csv", inputStream);

    try {
      File uploadedFile = service.files().create(fileMetadata, mediaContent)
                                .setSupportsAllDrives(true) 
                                .execute();
com.google.api.services.drive.model.Permission newPermission = new com.google.api.services.drive.model.Permission() ;
      newPermission.setType("user");
      newPermission.setRole("writer");
      newPermission.setEmailAddress("xyz@xyz.com");
        
      service.permissions().create(createdFileId, newPermission)
                .setSupportsAllDrives(true)
                .execute(); 

After the execution of 'service.permissions().create()', i'm getting below error:

400 Bad Request POST https://www.googleapis.com/drive/v3/files/16-wFWifo0HNZW6W1lujJiT1HO-1wLbK/permissions?supportsAllDrives=true { "code": 400, "errors": [ { "domain": "global", "message": "Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."", "reason": "invalidSharingRequest" } ], "message": "Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."" }

Really need inputs in this. Thanks in advance!

Ankur Jangra
  • 21
  • 1
  • 2

3 Answers3

4

Try setting the sendNotificationEmail to false when creating file permissions. This solved it for me allthough I am not quite sure what caused the error in the first place. Could have something to do with the service account's settings but I am just guessing. According to documentation the error can happen for several reasons but does not explicitly mention a solution for this specific message.

Hopefully this will help you!

zesbr
  • 41
  • 3
0

This is an issue with the file itself and not related to the api

if your file was flagged you can go to the drive web application and request that the file be reviewed again review

if the file is uploaded to the server account drive account I don't think you can request a review.

remember there are several types of files that drive probably won't let you share .zip is one

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I'm uploading through service account to a shared drive. So i'm able to view the file there and also able to share the file from drive web application. So no need to go for a review. The problem is that it's not getting shared by the code I uploaded above. There, i'm getting the above mentioned error. I'm wondering if there are any permissions required for the service account to enable it to send the notification emails to the user whom i want to share the file with. Note: It is a csv file. – Ankur Jangra Oct 18 '22 at 07:09
  • depends on if you have configured deligation through workspace. try taking the service account email address and share the file directly with the service account like you would any other user via the web app – Linda Lawton - DaImTo Oct 18 '22 at 14:58
0

Before clicking the "send" button, you might try unchecking the "Notify people" button.

By default, the "Notify people" button remains checked. You can uncheck the button and try. It worked for me.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34493355) – amycodes Jun 05 '23 at 22:01