Google Drive API v3迁移

Iva*_*nRF 17 google-drive-api

我决定从Google Drive API v2迁移到v3,这不是一件容易的事.即使谷歌写这篇文档,也有很多空白,网上没有太多关于此的信息.

我在这里分享我发现的东西.

Iva*_*nRF 40

首先,阅读官方文档:迁移到Google Drive API v3


下载

下载已更改.该字段downloadUrl不再存在.现在,可以通过以下方式实现:

service.files().get(fileId).executeMediaAndDownloadTo(outputStream);
Run Code Online (Sandbox Code Playgroud)

我尝试了新字段,webContentLink但它返回HTML内容,而不是文件的内容.换句话说,它为您提供了Drive Web UI的链接.


上传

上传只需要改词insertcreate,仅此而已.


垃圾桶/更新

我浪费了一些时间.过去很简单service.files().trash(fileId).execute().文件说

files.trash - > files.update with {'trashed':true}

示例代码用于update在V2使得get上档,设定了新的值,然后调用update.

在v3上,使用updatelike会抛出此异常:

{
  "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."
}
Run Code Online (Sandbox Code Playgroud)

解决方案是File仅为新值创建一个空设置:

File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();
Run Code Online (Sandbox Code Playgroud)

注意:Filecom.google.api.services.drive.model.File(不是java.io.File).


名单

返回的文件service.files().list()现在不包含信息,即每个字段都为空.如果您希望listv3在v2中表现得像,请像这样调用它:

service.files().list().setFields("nextPageToken, files");
Run Code Online (Sandbox Code Playgroud)

搜索文件的文档使用setFields("nextPageToken, files(id, name)")但没有关于如何获取文件的所有信息的文档.现在你知道了,只需要包含"文件".


字段

默认情况下不再返回完整资源.使用fieldsquery参数请求返回特定字段.如果未指定,则仅返回常用字段的子集.

最后一部分并非完全正确,因为setFields在某些情况下你被迫使用.例如,如果您使用,service.about().get().execute()您将收到此错误:

"The 'fields' parameter is required for this method."
Run Code Online (Sandbox Code Playgroud)

service.about().get().setFields("user, storageQuota").execute()例如,通过调用解决了这个问题.

在文档的最后提到:

fields查询参数应为其收益的方法来指定


对于其他更改,只需按照文档上的Google表格操作即可.

  • Re字段:"[有]关于如何定义这个新参数的信息不多." 2017年,我使用"字段"参数制作了2个视频:a)从API读取数据时(http://developers.googleblog.com/2017/04/using-field-masks-with-google-apis- for.html),和b)通过API更新数据时(http://developers.googleblog.com/2017/04/using-field-masks-with-update-requests.html) (2认同)