google-api-javascript-client:如何使用Drive API获取文件内容?

时间:2012-07-18 12:21:26

标签: google-api google-drive-api

首先,如果有问题/答案已经解决了我的问题,那么我真诚地为创建新问题而道歉。但是,我一直在寻找3天,但没有找到答案......

我的问题是,我不能为我的生活弄清楚如何拉取文件(任何文件)的内容。通过阅读文档,我发现我的返回文件资源对象应该有一个名为“downloadUrl”的属性,从中我应该能够访问文件内容。

返回给我的所有文件资源对象(通过gapi.client.request)都没有此字段/属性。下面是我用来获取文件的函数。 请有人帮我指点正确的方向吗?我必须在星期一之前完成这个演示,并且我已经坚持了2天....

以下是我的get函数的代码:

Client.getFileContent = function getFileContent() {
     gapi.client.load('drive', 'v2', function() {
          var request = gapi.client.request({
               path : '/drive/v2/files/1QmaofXyVqnw6ODXHE5KWlUTcWbA9KkLyb-lBdh_FLUs',
               method : 'GET',
               params : {
                    projection: "FULL"
               }
          });
          request.execute(function(response) {
               console.log(response);   
          });
     });
};

返回给我的文件资源对象没有downloadUrl属性。

根据要求,这是我为文本文件返回的响应对象。注意,我用“fileid”替换了一些id,以便在这里发布。

"kind": "drive#file",
   "id": "fileID",
   "etag": "\"-tJAWr_lbRQU2o8gZ0X7BCBIlVk/MTM0MjYyODQ1MTQ2Nw\"",
   "selfLink": "https://www.googleapis.com/drive/v2/files/fileID",
   "alternateLink": "https://docs.google.com/document/d/fileID/edit",
   "embedLink": "https://docs.google.com/document/d/fileID/preview",
   "thumbnailLink": "https://docs.google.com/feeds/vt?gd=true&id=fileID&v=1&s=AMedNnoAAAAAUAfLhbYIDsNIn40k7DfRYBsrquijmCii&sz=s220",
   "permissionsLink": "https://www.googleapis.com/drive/v2/files/fileID/permissions",
   "title": "Copied filed.txt",
   "mimeType": "application/vnd.google-apps.document",
   "labels": {
    "starred": false,
    "hidden": false,
    "trashed": false,
    "restricted": false,
    "viewed": true
   },
   "createdDate": "2012-07-18T16:20:51.132Z",
   "modifiedDate": "2012-07-18T16:20:51.467Z",
   "modifiedByMeDate": "2012-07-18T16:20:51.467Z",
   "lastViewedByMeDate": "2012-07-18T16:20:51.467Z",
   "parents": [
    {
     "kind": "drive#parentReference",
     "id": "0AAAYYkwdgVqHUk9PVA",
     "selfLink": "https://www.googleapis.com/drive/v2/files/fileID/parents/0AAAYYkwdgVqHUk9PVA",
     "parentLink": "https://www.googleapis.com/drive/v2/files/0AAAYYkwdgVqHUk9PVA",
     "isRoot": true
    }
   ],
   "exportLinks": {
    "application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=odt",
    "application/msword": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=doc",
    "text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=html",
    "application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=rtf",
    "text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=txt",
    "application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=pdf"
   },
   "userPermission": {
    "kind": "drive#permission",
    "etag": "\"-tJAWr_lbRQU2o8gZ0X7BCBIlVk/9STkNeCmz61YXorH3hoJimnEgfM\"",
    "id": "current",
    "role": "owner",
    "type": "user"
   },
   "quotaBytesUsed": "0",
   "ownerNames": [
    "Joshua.morine"
   ],
   "lastModifyingUserName": "Joshua.morine",
   "editable": true,
   "writersCanShare": true
  }

2 个答案:

答案 0 :(得分:8)

对于原生Google文档(Google电子表格,演示文稿等等),我们不提供downloadUrl,因为这些文件无法真正以原生格式下载为文件。相反,您必须使用 exportLinks 列表中的一个网址,该网址提供以几种不同的导出格式下载Google文档的网址。

在您的情况下,可以使用以下Google文档:

"exportLinks": {
    "application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=odt",
    "application/msword": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=doc",
    "text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=html",
    "application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=rtf",
    "text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=txt",
    "application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=fileID&exportFormat=pdf"
   }

答案 1 :(得分:3)

您正在寻找的元数据功能实际上是:

request = gapi.client.drive.files.get({
    'fileId': fileId
});

这个产生了你所指的downloadUrl的结果。然后,使用任何HTTP请求都可以轻松获取文件。