Я обнаружил, что пытался экспортировать документ как документ Google, поэтому у меня возникали ошибки при попытке использовать метод executeMediaAndDownloadTo. Экспорт в виде открытого текста или в другом формате, отличном от Google, кажется, необходим для этого. Я уверен, что это вызовет какие-то проблемы с форматированием, но должно быть возможно обойти их. Вот основное использование того, чем я закончил.
public void updateGoogleDocFile(String fileId, String newContent) throws IOException
{
try {
//First retrieve the file from the API as text file
java.io.File tempDataFile = java.io.File.createTempFile("googleDocsIncomingFile", ".txt");
OutputStream os = new FileOutputStream(tempDataFile);
//Convert data to bytes and write to new file
byte[] fileBytes = downloadGoogleDocFile(fileId).toByteArray();
os.write(fileBytes);
//Get new data to append, convert to bytes, and write to file
byte[] newConentBytes = newContent.getBytes();
os.write(newConentBytes);
os.flush();
os.close();
//File's new metadata.
File newFile = new File();
//newFile.setName(fileName);
//newFile.setDescription("mydescription");
//newFile.setMimeType(MIME_TYPE.GoogleDocsDocument.toString());
// Send the request to the API.
FileContent mediaContent = new FileContent(MIME_TYPE.GoogleDocsDocument.toString(), tempDataFile);
SERVICE.files().update(fileId, newFile, mediaContent).execute();
} catch (IOException e)
{
System.out.println("An error occurred while trying to update the google docs file: " + e);
}
}
public ByteArrayOutputStream downloadGoogleDocFile(String fileId) throws IOException
{
Export export = SERVICE.files().export(fileId, MIME_TYPE.PlainText.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
export.executeMediaAndDownloadTo(out);
return out;
}