get file content of google docs using google drive API v3

31,264

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
31,264
Johnny
Author by

Johnny

Updated on July 10, 2022

Comments

  • Johnny
    Johnny almost 2 years

    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.

    • Johnny
      Johnny over 7 years
      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
    Johnny over 7 years
    although it works for getting the last version, I couldn't find export for revisions of a native google doc
  • Jzou
    Jzou about 6 years
    Nice and clean explanation. You saved my day, Phu!
  • Maroš Beťko
    Maroš Beťko over 4 years
    @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.
  • phuwin
    phuwin over 4 years
    @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.
  • Maroš Beťko
    Maroš Beťko over 4 years
    @phuwin Here is the related question stackoverflow.com/questions/57240830/…
  • ravioli
    ravioli almost 4 years
    Updated link for export mime types: developers.google.com/drive/api/v3/ref-export-formats