Search code examples
javagdatagoogle-docs-apigoogle-drive-api

retrieve text document content with Docs api JAVA


How can i get the document content in java, with the google docs api. this is my code.

      MediaContent mc = (MediaContent) entry.getContent();
      String UrlForDownload = mc.getUri()+"&exportFormat=txt&format=txt";
        mc.setUri(UrlForDownload);
    MediaSource ms = client.getMedia(mc);

But am not getting the content, which is the right way to have it? thx in advance


Solution

  • I'm not sure if there is a good way to get the export URL using the Google Docs API. However you could try using this URL which is part of the Drive API v2:

    https://docs.google.com/feeds/download/documents/export/Export?id=<docID>&exportFormat=txt
    

    The only issue is that the URL is suposed to be obscure (though not likely to change soon), in theory you should just use the export link which is part of the File's metadata in the Google Drive API:

    "exportLinks": {
      ...
      "text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=1X24S59c2o4pXW9HkAzG-qnq9uegsLNXfnG_2usiVGDs&exportFormat=txt",
      ...
    

    But for this you would need to be using the Google Drive API instead fo the Google Document List API.