Daniele Romanella
Daniele Romanella

Reputation: 94

Error while creating Google Drive folder into another folder

I need create GDrive folder into another GDrive folder.

There's the code:

private static String createDriveFolder(String folderName, String mainFolder, Drive service) throws IOException {
    File fileMetadata = new File();
    fileMetadata.setName(folderName);
    fileMetadata.setParents(Collections.singletonList(mainFolder));
    fileMetadata.setMimeType("application/vnd.google-apps.folder");

    File file = service.files().create(fileMetadata)
            .setFields("id, parent")
            .execute();
    System.out.println("Folder ID: " + file.getId());
    return file.getId();
}

I get this error...

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "fields",
    "locationType" : "parameter",
    "message" : "Invalid field selection parent",
    "reason" : "invalidParameter"
  } ],
  "message" : "Invalid field selection parent"
}

Please help me...

Upvotes: 1

Views: 133

Answers (1)

Tanaike
Tanaike

Reputation: 201643

How about this modification? I think that from the error message, the reason of the issue is due to the value of fields.

From:

.setFields("id, parent")

To:

.setFields("id, parents")

Note:

  • Please modify parent to parents. At Google Drive, in the current stage, a file and folder have the multiple parents. So parents is used. But we can see the following announce. Ref

    Beginning Sept. 30, 2020, it will no longer be possible to place an item in multiple folders; every item will have exactly one location. In the new model, shortcuts can be used to organize content in multiple hierarchies. This simplification of Drive's folder structure and sharing models will result in a change in the way some Google Drive API endpoints behave.

    • I'm not sure whether parents will be changed to parent. So about this, I would like to confirm the change of the Drive API after Sept. 30, 2020.

Reference:

Upvotes: 2

Related Questions