无法使用Drive API v3更新Google云端硬盘文件 - 资源正文包含不可直接写入的字段 [英] Unable to update Google Drive files using Drive API v3 -- The resource body includes fields which are not directly writable

查看:294
本文介绍了无法使用Drive API v3更新Google云端硬盘文件 - 资源正文包含不可直接写入的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Google Drive API(v3)在Google云端硬盘中更新文档



我已阅读此迁移指南:
Google Drive API v3迁移



然后编码它以创建一个新的空File(),其细节我想更新
,然后用这个和文件ID调用execute()。
但我仍然收到错误。任何人都可以指出我做错了什么?
非常感谢!!



错误:

  { 
code:403,
errors:[{
domain:global,

message:资源正文包含字段它们不是直接可写的。,

reason:fieldNotWritable
}],
message:资源体包含不可直接写入的字段。
}

以下代码片段:

  File newFileDetails = new File(); 
FileList result = service2.files().list()
.setPageSize(10)
.setFields(nextPageToken,files(id,name))
.execute( );

列表< File> files = result.getFiles();

if(files == null || files.size()== 0){

System.out.println(No files found。);

} else {

System.out.println(Files:); (文件file:文件){

if(file.getName()。equals(first_sheet)){

系统。 out.printf(%s(%s)\\\
,file.getName(),file.getId());


newFileDetails.setShared(true);


service2.files()。update(file.getId(),newFileDetails).execute();

}

}

}


解决方案

参考 https://developers.google.com/drive/v3/reference/files#resource-representations ,您可以看到共享不是可写领域。如果你仔细想想,这很有道理。您可以通过添加新权限来共享文件,并且可以通过阅读共享属性来检查文件是否已共享。但是说一个文件是共享的,而不是通过实际共享文件是没有意义的。

I am trying to use Google Drive API (v3) to make updates to documents in Google Drive.

I have read this migration guide: Google Drive API v3 Migration

And coded it to make a new empty File() with the details I want to update and then calling execute() with that and the file ID. But i am still getting an error. Can anyone point out where I am doing wrong? thanks alot!!

Error:

{
    "code" : 403,
    "errors" : [{
        "domain" : "global",

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

        "reason" : "fieldNotWritable"
    }],
    "message" : "The resource body includes fields which are not directly writable."
}

Code snippet below:

File newFileDetails = new File();        
FileList result = service2.files().list()
    .setPageSize(10)    
    .setFields("nextPageToken, files(id, name)")
    .execute();

List<File> files = result.getFiles();

if (files == null || files.size() == 0) {

    System.out.println("No files found.");

} else {

   System.out.println("Files:");

   for (File file : files) {

      if (file.getName().equals("first_sheet")) {

         System.out.printf("%s (%s)\n", file.getName(), file.getId());


         newFileDetails.setShared(true);


         service2.files().update(file.getId(), newFileDetails).execute();

      }

   }

}

解决方案

Referring to https://developers.google.com/drive/v3/reference/files#resource-representations, you can see that shared isn't a writable field. If you think about it, this makes perfect sense. You can share a file by adding a new permission, and you can check if a file has been shared by reading the shared property. But saying a file is shared, other than by actually sharing it, makes no sense.

这篇关于无法使用Drive API v3更新Google云端硬盘文件 - 资源正文包含不可直接写入的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆