Google drive api 使用google drive API v3获取google文档的文件内容

Google drive api 使用google drive API v3获取google文档的文件内容,google-drive-api,Google Drive Api,有没有办法使用google drive API v3获取本机文件(google文档)的内容?我知道API v2支持exportLinks属性,但它不再工作或已被删除。对于API的v3,您可以使用导出方法如果使用,您将无法获得任何允许您下载中所述文件的链接 例如,使用try it,我只得到了一个MiMetype响应,但没有可下载的链接: [application/vnd.oasis.opendocument.text data] 解决方法是直接下载。只需将文件\u ID替换为您的谷歌文档文件I

有没有办法使用google drive API v3获取本机文件(google文档)的内容?我知道API v2支持exportLinks属性,但它不再工作或已被删除。

对于API的v3,您可以使用导出方法

如果使用,您将无法获得任何允许您下载中所述文件的链接

例如,使用try it,我只得到了一个MiMetype响应,但没有可下载的链接:

[application/vnd.oasis.opendocument.text data] 
解决方法是直接下载。只需将
文件\u ID
替换为您的谷歌文档文件ID,并在浏览器中执行即可。通过这个,我可以导出谷歌文档文件

https://docs.google.com/document/d/FILE_ID/export?format=doc 

此解决方法归功于。

您还可以使用文件的
webContentLink
属性下载驱动器中包含二进制内容的文件(非谷歌驱动器文件)。发件人:

用于在浏览器中下载文件内容的链接。这是 仅适用于驱动器中包含二进制内容的文件

例如(我使用方法从我的文件中检索
webContentLink
):


对于google驱动器文件,可以使用导出方法获取这些文件:
此方法需要一个具有两个必需属性(
fileId
mimeType
)的对象作为参数。 可以查看或查看可用的
mimeType
s列表(感谢@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);
})

您可以使用
gapi.client.drive.files.get
alt:“media”
读取非google文档文件内容(例如文本文件)。我的例子是:

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;
}

虽然它适用于获取最新版本,但我找不到本机google文档修订版的导出。虽然一些答案适用于获取本机google文档的最新版本,但我找不到本机google文档修订版的导出。你救了我一天,福@phuwin我正在尝试从驱动器下载纯文本文件,而您提供的第一个解决方案在我尝试
获取
webContentLink
时由于CORS而失败。第三个选项返回原始字符串中的内容,该字符串包含文件内容和包装在
WebkitFormBoundary
中的一些附加信息,我不确定如何正确解析这些信息。我应该提到的是,我的应用程序纯粹是客户端浏览器应用程序。@MaroťBeťko我恐怕无法回答你的问题。您可能需要在不同的stackoverflow帖子中添加一个新问题,并提供有关您的情况的更多信息。或者您可以检查
@noogui
答案。@这是导出mime类型的相关问题更新链接:
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;
}