1
votes

How to set the app properties of a file using Google Drive v3 in java?

The reference says: "files.update with {'appProperties':{'key':'value'}}", but I don't understand how to apply that to my java code.

I've tried things like

file = service.files().create(body).setFields("id").execute();
Map<String, String> properties = new HashMap<>();
properties.put(DEVICE_ID_KEY, deviceId);
file.setAppProperties(properties);
service.files().update(file.getId(), file).setFields("appProperties").execute();

but then I get an error that "The resource body includes fields which are not directly writable."

And to get the data:

File fileProperty = service.files().get(sFileId).setFields("appProperties").execute();

So how to set and get the properties for the file? Thanks! :)

Edit I tried

file = service.files().create(body).setFields("id").execute();
Map<String, String> properties = new HashMap<>();
properties.put(DEVICE_ID_KEY, deviceId);
file.setAppProperties(properties);
service.files().update(file.getId(), file).execute();

but I still get the same error message.

4

4 Answers

2
votes

To avoid this error on v3

"The resource body includes fields which are not directly writable."

when calling update, you need to create an empty File with the new changes and pass that to the update function.

I wrote this and other notes as a v3 Migration Guide here.

1
votes

The Drive API client for Java v3 indicates that the File.setAppProperties will require a Hashmap<String,String> parameter. Try to remove the setFields("appProperties") call since you are trying to overwrite appProperties itself (you're still calling Update at this time).

When retrieving appProperties, you'll just need to call getAppProperties.

Hope this helps!

0
votes
    File fileMetadata = new File();       
    java.io.File filePath = new java.io.File(YOUR_LOCAL_FILE_PATH);

    Map<String, String> map = new HashMap<String, String>();
    map.put(YOUR_KEY, YOUR_VALUE); //can be filled with custom String
    fileMetadata.setAppProperties(map);     

    FileContent mediaContent = new FileContent(YOUR_IMPORT_FORMAT, filePath);
    File file = service.files().create(fileMetadata, mediaContent)
            .setFields("id, appProperties").
            .execute();

YOUR_IMPORT_FORMAT, fill this with the value in this link, https://developers.google.com/drive/api/v3/manage-uploads, there is explanation below the example code

setFields("id, appProperties"), fill this with the value in this link: https://developers.google.com/drive/api/v3/migration, this the most important part I think, if you don't set the value in the setFields method your additional input will not be written

0
votes

With version v3, to add or update appProperties for an existing file and without this error:

"The resource body includes fields which are not directly writable."

You should do:

String fileId = "Your file id key here";
Map<String, String> appPropertiesMap = new HashMap<String, String>();
appPropertiesMap.put("MyKey", "MyValue");
appPropertiesMap.put("MySecondKey", "any value");

//set only the metadata you want to change
//do not set "id" !!! You will have "The resource body includes fields which are not directly writable." error
File fileMetadata = new File();
fileMetadata.setAppProperties(appPropertiesMap);

File updatedFileMetadata = driveService.files().update(fileId, fileMetadata).setFields("id, appProperties").execute();
System.out.printf("Hey, I see my appProperties :-) %s \n", updatedFileMetadata.toPrettyString());