我决定从Google Drive API v2迁移到v3,这并非易事。甚至以为Google编写了documentation字词,仍然存在很多空白,并且在网络上没有太多有关此信息。
我在这里分享我发现的东西。
最佳答案
首先,请阅读官方文档:v2 to v3 reference | Drive API v3 versus v2 | Migrate to v3
下载
Download已更改。字段downloadUrl
不再存在。现在,可以通过以下方式实现:
service.files().get(fileId).executeMediaAndDownloadTo(outputStream);
我尝试了新字段
webContentLink
,但是它返回HTML内容,而不是文件的内容。换句话说,它为您提供了到云端硬盘网络界面的链接。上载
上载仅需将单词
insert
更改为create
,仅此而已。垃圾桶/更新
我浪费了一些时间。曾经是一个简单的
service.files().trash(fileId).execute()
。医生说
files.trash-> files.update,带有{'trashed':true}
v2上
update
的example code在文件上创建get
,设置新值,然后调用update
。在v3上,像这样使用
update
会引发此异常:{
"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."
}
解决方案是创建一个空的
File
,仅设置新值:File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();
注意:
File
指的是com.google.api.services.drive.model.File
(不是java.io.File
)。清单
service.files().list()
返回的文件现在不包含信息,即每个字段为空。如果您希望v3上的list
行为与v2相同,请按以下方式调用它:service.files().list().setFields("nextPageToken, files");
Search for files and folders上的文档使用
setFields("nextPageToken, files(id, name)")
,但是没有有关如何获取文件的所有信息的文档。现在,您只需添加“文件”即可。领域
默认情况下,不再返回完整资源。使用
fields
查询参数来请求返回特定的字段。如果未指定,则仅返回常用字段的子集。最后一部分并不完全正确,因为在某些情况下您不得不使用
setFields
。例如,如果使用service.about().get().execute()
,则会出现此错误:"The 'fields' parameter is required for this method."
例如,可以通过调用
service.about().get().setFields("user, storageQuota").execute()
来解决。在文档末尾提到:
对于返回的方法,应指定
fields
查询参数对于其余的更改,只需遵循文档上的Google表格即可。