有没有办法使用google drive API v3获取原生文件(google docs)的内容?我知道v2接口通过exportLinks属性支持这一点,但是它不再工作了,或者已经被删除了。
发布于 2016-09-08 05:23:43
对于接口的v3,可以使用导出方法https://developers.google.com/drive/v3/reference/files/export
发布于 2016-09-09 09:59:14
您还可以使用文件的webContentLink
属性在drive中下载包含二进制内容的文件(非google驱动器文件)。来自https://developers.google.com/drive/v3/reference/files
用于在浏览器中下载文件内容的链接。此选项仅适用于驱动器中包含二进制内容的文件。
一个示例(我使用get()
方法从我的文件中检索webContentLink
):
gapi.client.drive.files.get({
fileId: id,
fields: 'webContentLink'
}).then(function(success){
var webContentLink = success.result.webContentLink; //the link is in the success.result object
//success.result
}, function(fail){
console.log(fail);
console.log('Error '+ fail.result.error.message);
})
对于谷歌驱动器文件,可以使用导出方法来获取这些文件:https://developers.google.com/drive/v3/reference/files/export
此方法需要一个具有两个强制属性(fileId
和mimeType
)作为参数的对象。可以在here或here上看到可用的mimeType
列表(感谢@ravioli)
示例:
gapi.client.drive.files.export({
'fileId' : id,
'mimeType' : 'text/plain'
}).then(function(success){
console.log(success);
//success.result
}, function(fail){
console.log(fail);
console.log('Error '+ fail.result.error.message);
})
您可以使用带有alt:"media"
的gapi.client.drive.files.get
读取非google文档文件内容(例如,文本文件)。Official example。我的例子:
function readFile(fileId, callback) {
var request = gapi.client.drive.files.get({
fileId: fileId,
alt: 'media'
})
request.then(function(response) {
console.log(response); //response.body contains the string value of the file
if (typeof callback === "function") callback(response.body);
}, function(error) {
console.error(error)
})
return request;
}
发布于 2016-09-09 06:26:06
如果您使用files.export,您将不会获得任何允许您按照v3 Migration guide中的说明下载该文件的链接。
例如,使用try-it,我只得到了一个MiMetype响应,但没有可下载的链接:
[application/vnd.oasis.opendocument.text data]
解决此问题的方法是直接下载。只需将FILE_ID
替换为您的Google Doc fileID并在浏览器中执行即可。通过这个,我能够导出Google文档文件。
https://docs.google.com/document/d/FILE_ID/export?format=doc
将此解决方法归功于labnol's guide。
https://stackoverflow.com/questions/39381563
复制相似问题