Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google-api-javascript-client : How to get contents of file using Drive API?

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
  }
like image 466
JoshuaMorine Avatar asked Dec 20 '22 18:12

JoshuaMorine


2 Answers

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"
   }
like image 158
Nicolas Garnier Avatar answered Dec 26 '22 22:12

Nicolas Garnier


The meta-data function you are looking for is actually:

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

This one produces a result with the downloadUrl that you're referring to. Then it's easy to grab the file using any HTTP request.

like image 37
Gojko Adzic Avatar answered Dec 26 '22 22:12

Gojko Adzic