Google Drive API Java:尝试将文件移至垃圾箱时出现错误

问题描述 投票:0回答:1

我正在尝试使用 Java Google Drive API 将文件移至垃圾箱。这是我尝试做的事情:

代码:

        val file = service.files().get(fileId).execute()
        file.setTrashed(true)
        service.files().update(fileId, file).execute()

错误:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
POST https://www.googleapis.com/drive/v3/files/0B0XJAu1tqe0ZT01ZSzRLb3Zfa3c
{
  "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."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:439)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:525)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:466)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:576)
    at DriveQuickstart.main(DriveQuickstart.kt:97)
java kotlin google-drive-api
1个回答
0
投票

service.files().get(fileId).execute()
返回的文件包含很多字段,其中一些字段必须是不可写的。

我设法通过明确指出不返回任何其他字段来修复此错误:

        val file = service.files().get(fileId).setFields("").execute()
        file.setTrashed(true)
        service.files().update(fileId, file).execute()
© www.soinside.com 2019 - 2024. All rights reserved.