get file content of google docs using google drive API v3

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.


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

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