4

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
  }
JoshuaMorine
  • 43
  • 1
  • 1
  • 5
  • Can you also paste the content of response so that we can take a look? Please make sure to strip off any private data. FYI, only files that have content stored in Drive (images, pdf, etc.) will have a downloadUrl. – Alain Jul 18 '12 at 16:11
  • Wow. Thank you Alain. I just uploaded a .png to the drive to check what you said, and sure enough there was a downloadUrl. So, how is one supposed to get content from text files and other documents? I will post the response object I get in a few minutes... – JoshuaMorine Jul 19 '12 at 06:58
  • From the metadata you posted, you are trying to get the content of a Google Document. This is not possible directly (as the content is stored in a private format), but you can use the various exportLinks to export in a format understood by your application. – Alain Jul 19 '12 at 17:36

2 Answers2

8

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"
   }
Nicolas Garnier
  • 12,134
  • 2
  • 42
  • 39
  • Okay, thats what I thought, to use the exportLinks that is. However, could you point me in the right direction for accessing these links? I keep getting a (405) method not found... I tried using the CORS request method as described in the api docs. I'm really confused. Sorry if I missed something obvious... – JoshuaMorine Jul 23 '12 at 06:22
  • However, based on this info and what my digging has revealed, accessing the exportLinks is not what I want. I actually want to access the Google Docs and Spreadsheets. So now I just have to figure out how to work with the Docs API in js. Or, is that possible? – JoshuaMorine Jul 23 '12 at 07:39
  • @JoshuaMorine Could you please have a look at this question: http://stackoverflow.com/questions/11578020/how-to-programmatically-manipulate-native-google-doc-files/11588814#comment15371184_11588814 and let me know if that answers it. – Nicolas Garnier Jul 23 '12 at 18:13
  • Thank you for the help and information. This has answered all of my javascript questions, although, the company's decision is to forget using JS and work from a Java standpoint. Which means I have to learn Java now... Thanks again though! – JoshuaMorine Aug 10 '12 at 09:39
3

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.

Matt
  • 74,352
  • 26
  • 153
  • 180
Gojko Adzic
  • 1,241
  • 10
  • 9
  • 1
    The download link does not exist for Google doc files like spreadsheets or docs -- it only appears for files that have been uploaded to drive. – Jude Osborn Nov 27 '14 at 00:50