google-api-javascript-client:如何使用Drive API获取文件的内容? [英] google-api-javascript-client : How to get contents of file using Drive API?

查看:80
本文介绍了google-api-javascript-client:如何使用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(函数(响应){
console.log(响应);
});
});
};

返回给我的文件资源对象没有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:复制filed.txt,
mimeType:application / vnd.google-apps.document,
labels:{
starred:false,
隐藏:false,
trashed:false,
restricted:false,
查看: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/down load / 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
}


解决方案

对于本地Google文档(Google Spreadsheet,Presentation等),我们不提供downloadUrl,因为这些文档不能真正作为其原生文件f ORMAT。相反,您必须使用 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/导出?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/下载/ documents / export / Export?id = fileID& exportFormat = pdf


First off, if there is a question/answer that solves my problem already then I sincerely apologize for creating a new one. However, I have been searching for 3 days now, and have not found an answer...

My problem is, I cannot for the life of me figure out how to pull the contents of a file(any file). From reading the docs I've discovered that my returned file resource object is supposed to have a property named "downloadUrl", and from this I should be able to access the file contents.

None of the file resource objects that are returned to me(via gapi.client.request) have this field/property. Below is the function I am using to get a file. Can someone please help point me in the right direction? I have to have this demo done by Monday and I've been stuck on this for 2 days....

Here is the code for my get function :

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

The file resource object that is returned to me does not have the downloadUrl property.

As requested, here is the response object I get back for a text file. Note, I replaced some of the ids with "fileid" for posting here.

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

解决方案

For native Google documents (Google Spreadsheet, Presentation etc...) we don;t provide a downloadUrl as these can't really be downloaded as files in their native format. Instead you'll have to use one of the URLs in the list of exportLinks which provides URLs to download the Google Documents in a few different export formats.

In your case, a Google Documents the following can be used:

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

这篇关于google-api-javascript-client:如何使用Drive API获取文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆