【问题标题】:How to use appProperties in Google Drive api v3?如何在 Google Drive api v3 中使用 appProperties?
【发布时间】:2019-11-16 21:41:50
【问题描述】:

如何在 java 中使用 Google Drive v3 设置文件的应用属性?

参考资料说:“files.update with {'appProperties':{'key':'value'}}”,但我不明白如何将其应用于我的 java 代码。

我尝试过类似的东西

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();

但随后我收到“资源主体包含不可直接写入的字段”的错误消息。

并获取数据:

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

那么如何设置和获取文件的属性呢? 谢谢! :)

编辑 我试过了

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();

但我仍然收到相同的错误消息。

【问题讨论】:

    标签: java google-drive-api


    【解决方案1】:

    为了避免在 v3 上出现此错误

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

    在调用update 时,您需要使用新更改创建一个空的File 并将其传递给update 函数。

    我以v3 Migration Guide here 的形式写了这篇笔记和其他笔记。

    【讨论】:

      【解决方案2】:

      Drive API client for Java v3 表示File.setAppProperties 将需要Hashmap&lt;String,String&gt; 参数。尝试删除 setFields("appProperties") 调用,因为您正试图覆盖 appProperties 本身(此时您仍在调用 Update)。

      在检索appProperties 时,您只需调用getAppProperties

      希望这会有所帮助!

      【讨论】:

      • 我试过 file = service.files().create(body).setFields("id").execute(); Map 属性 = new HashMap(); properties.put(DEVICE_ID_KEY, deviceId); file.setAppProperties(属性);文件 fileProperty = service.files().update(file.getId(), file).execute();但我仍然遇到同样的错误。
      【解决方案3】:
          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,填写此链接中的值https://developers.google.com/drive/api/v3/manage-uploads,示例代码下方有说明

      setFields("id, appProperties"),用这个链接中的值填充:https://developers.google.com/drive/api/v3/migration,这是我认为最重要的部分,如果你不在 setFields 方法中设置值,你的额外输入将不会写出来

      【讨论】:

        【解决方案4】:

        使用 v3 版本,为现有文件添加或更新 appProperties 且不会出现此错误:

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

        你应该这样做:

        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());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-23
          • 2020-10-23
          • 2020-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多