Google 驱动器 API v3 迁移
Google Drive API v3 Migration
我决定从 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
但它是 returns HTML 内容,而不是文件内容。换句话说,它为您提供 link 到 Drive web UI.
上传
上传只需要将insert
这个词改成create
,仅此而已。
垃圾/更新
我在这个上浪费了一些时间。曾经是一个简单的service.files().trash(fileId).execute()
。
文档说
files.trash -> files.update with {'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)")
,但没有关于如何获取文件的所有信息的文档。现在你知道了,只要包括 "files".
字段
Full resources are no longer returned by default. Use the fields
query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.
最后一部分并不完全正确,因为在某些情况下您被迫使用 setFields
。例如,如果您使用 service.about().get().execute()
,您将收到此错误:
"The 'fields' parameter is required for this method."
例如通过调用service.about().get().setFields("user, storageQuota").execute()
解决。
文档末尾提到:
The fields
query parameter should be specified for methods which return
对于其余的更改,只需按照文档中的 Google table 即可。
我没有发表评论的名誉(抱歉),但是接受的答案 service.about().get().setFields("user, storageQuota").execute()
中提供的代码行没有 运行,而是抛出属性错误:
about = service.about().get().setFields("user", "storageQuota").execute()
AttributeError: 'HttpRequest' object has no attribute 'setFields'
相反,该行应为:
service.about().get(fields = "user, storageQuota").execute()
我决定从 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
但它是 returns HTML 内容,而不是文件内容。换句话说,它为您提供 link 到 Drive web UI.
上传
上传只需要将insert
这个词改成create
,仅此而已。
垃圾/更新
我在这个上浪费了一些时间。曾经是一个简单的service.files().trash(fileId).execute()
。
文档说
files.trash -> files.update with {'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)")
,但没有关于如何获取文件的所有信息的文档。现在你知道了,只要包括 "files".
字段
Full resources are no longer returned by default. Use the
fields
query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.
最后一部分并不完全正确,因为在某些情况下您被迫使用 setFields
。例如,如果您使用 service.about().get().execute()
,您将收到此错误:
"The 'fields' parameter is required for this method."
例如通过调用service.about().get().setFields("user, storageQuota").execute()
解决。
文档末尾提到:
The
fields
query parameter should be specified for methods which return
对于其余的更改,只需按照文档中的 Google table 即可。
我没有发表评论的名誉(抱歉),但是接受的答案 service.about().get().setFields("user, storageQuota").execute()
中提供的代码行没有 运行,而是抛出属性错误:
about = service.about().get().setFields("user", "storageQuota").execute()
AttributeError: 'HttpRequest' object has no attribute 'setFields'
相反,该行应为:
service.about().get(fields = "user, storageQuota").execute()