16

Is there any way to get the content of native files (Google Docs) using Google Drive API v3? I know API v2 supports this with the exportLinks property, but it doesn't work anymore or has been removed.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Johnny
  • 473
  • 1
  • 4
  • 10
  • although some of the answers works for getting the last version of a native google doc, I couldn't find export for revisions of a native google doc – Johnny Sep 10 '16 at 04:16

3 Answers3

21

You can also download files with binary content (non google drive file) in Drive using the webContentLink attribute of a file. From https://developers.google.com/drive/v3/reference/files:

A link for downloading the content of the file in a browser. This is only available for files with binary content in Drive.

An example (I use method get() to retrieve the webContentLink from my file):

gapi.client.drive.files.get({
    fileId: id,
    fields: 'webContentLink'
}).then(function(success){
    var webContentLink = success.result.webContentLink; //the link is in the success.result object
    //success.result    
}, function(fail){
    console.log(fail);
    console.log('Error '+ fail.result.error.message);
})

With google drive files, the export method can be used to get those files: https://developers.google.com/drive/v3/reference/files/export
This method needs an object with 2 mandatory attributes (fileId, and mimeType) as parameters. A list of available mimeTypes can be seen here or here (thanks to @ravioli)

Example:

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

You can read non google doc file content (for example a text file) with gapi.client.drive.files.get with alt:"media". Official example. My example:

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;
}
phuwin
  • 3,130
  • 4
  • 26
  • 49
  • 1
    Nice and clean explanation. You saved my day, Phu! – Jzou Apr 25 '18 at 20:11
  • @phuwin I'm trying to dowload a plain text file from drive and the first solution you provided fails because of CORS when I try to `GET` the `webContentLink`. The third option returns the contents in a raw string containing the file contents and some additional info wrapped inside a `WebkitFormBoundary` which I'm not sure how to parse properly. I should mention that my app is purely client-side browser app. – Maroš Beťko Jul 29 '19 at 18:24
  • @MarošBeťko I'm afraid I have no clue to answer your question. You might need to add a new question in a different stackoverflow post with more information about your situation. Or you can check `@noogui` answer. – phuwin Jul 30 '19 at 05:12
  • @phuwin Here is the related question https://stackoverflow.com/questions/57240830/google-drive-api-download-file-within-drive-appdata-scope – Maroš Beťko Jul 30 '19 at 08:20
  • 1
    Updated link for export mime types: https://developers.google.com/drive/api/v3/ref-export-formats – ravioli Jun 01 '20 at 05:11
7

For v3 of the api, you can use the export method https://developers.google.com/drive/v3/reference/files/export

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • although it works for getting the last version, I couldn't find export for revisions of a native google doc – Johnny Sep 10 '16 at 04:14
6

If you use files.export, you won't get any link that will let you download the file as stated in v3 Migration guide.

For example using the the try-it, I only got a MiMetype response but no downloadable link:

[application/vnd.oasis.opendocument.text data] 

Workaround for this is a direct download. Just replace FILE_ID with your Google Doc fileID and execute it in the browser. Through this, I was able to export Google docs files.

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

Credits to labnol's guide for the workaround.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56