iixi
iixi

Reputation: 580

can not get content of document with google drive sdk

I can upload files to Google Drive using the Google Drive SDK but can't get the download to work. I have a document on google drive that I want to download and read the text in its content. I can get the file with this code:

    file = drive.files().get(fileId).execute();

The downloadUrl is always null. I don't know where I read it but someone said that Google documents does not have a downloadUrl and you should use the export links instead.

    String downloadUrl = driveFile.getExportLinks().get("text/plain");
    if (downloadUrl != null && downloadUrl.length() > 0) {
        try {
            HttpResponse resp = 
                    drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
            content = new Scanner(resp.getContent()).useDelimiter("\\A").next();
        } catch (java.util.NoSuchElementException e) {
            return "";
        }
    }

After running this code I do not get the content in my text file but an html representation of something looking like this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to Google Docs</title>
<style type="text/css">
  html, body, div, h1, h2, h3, h4, h5, h6, p, img, dl,
  dt, dd, ol, ul, li, table, tr, td, form, object, embed,
  article, aside, canvas, command, details, fieldset,
  ....

Can someone please explain how to read the content of a document from google drive?

Edit

So I can not figure out how to get the download to work with the Google Drive SDK. I did however get it to work with this code instead:

String downloadUrl = driveFile.getExportLinks().get("text/plain");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(downloadUrl);
get.setHeader("Authorization", "Bearer " + token);
org.apache.http.HttpResponse response = client.execute(get);
InputStream inputStream = response.getEntity().getContent();
content = new java.util.Scanner(inputStream).useDelimiter("\\A").next();

To bad when I get everything else I need to do to work with the Drive SDK. If anybody can figure out why this is working but not the code I use with the SDK please tell.

Upvotes: 3

Views: 1187

Answers (1)

pinoyyid
pinoyyid

Reputation: 22306

Try 'text/html'. You're exporting a rich text document, so html makes more sense than plain.

Upvotes: 1

Related Questions