CC7589
CC7589

Reputation: 83

Document Viewer in Google Drive SDK - Java

I have successfully implemented Google service accounts with the Google Drive. Now, I can upload files to the service account. But I can't figure out how to show the file in an embedded form on my website.

https://doc-04-0g-docs.googleusercontent.com/docs/securesc/bc8ug145a9ddo935ikfpgr3jg35j66bd/r8o9fi9jati2in6m7ad70ppjqif4dq7n/1381514400000/05451613566453688664/05451613566453688664/0B20JX1z76nY6VjVrUl9rai16TW8?h=16653014193614665626&e=download&gd=true

https://docs.google.com/uc?id=0B20JX1z76nY6VjVrUl9rai16TW8&export=download

https://docs.google.com/file/d/0B20JX1z76nY6VjVrUl9rai16TW8/edit?usp=drivesdk

These are the downloadUrl, WebContentLink and AlternateLink respectively of the file uploaded. I'm unable to open these links in my browser. The code used to upload the file is:

java.io.File fileContent = new java.io.File("E:\\test.txt");
            File fileMetadata = new File();
            fileMetadata.setTitle(fileContent.getName());
            InputStreamContent mediaContent = new InputStreamContent("text/plain", new BufferedInputStream(
                    new FileInputStream(fileContent)));
            mediaContent.setLength(fileContent.length());

            Drive.Files.Insert insert = getDriveService().files().insert(fileMetadata, mediaContent);

            MediaHttpUploader uploader = insert.getMediaHttpUploader();
            uploader.setDirectUploadEnabled(true);
            File file = insert.execute();

How can I set the file property to public so that it is accessible by all the users on my website without their login? Also, I don't want the users to download this file.

Thanks

Upvotes: 2

Views: 7771

Answers (2)

Lambda
Lambda

Reputation: 1640

AlternateLink is the one that allow you to open for view or edition a document. Obviously you have to set permissions. setting permission is relatively easy.

gdrive api link

careful, it is a list so you have to make sure you don't keep inserting new permissions. with googledrive you can set edit or view to anyonewithTHElink. (anyone) and this link is AlternateLink.

Unfortunately I have not been able to find a way to stop user from downloading the file from google document editor file>download. if you find one, please let me know.

HOWEVER AlternateLink gives you your:

https://docs.google.com/file/d/0B20JX1z76nY6VjVrUl9rai16TW8/edit?usp=drivesdk

I believe this is outdated.

I use this in an iframe and the iframe does not allow for google correcting the url.

The current url ( Google changes it every few months) is ?pl=1 instead of ?usp=drivesdk. so if you open the link in a new tab google will correct this for you, but you have to manually change it in an iframe.

you will notice a different behaviors in Chrome vs the rest. ?usp=drivesdk will still work in most browser but will not work in chrome if you did not sign in with a google account.

so in short don't trust AlternateLink completely you may have to rewrite part of the url. if you use iframe.

Upvotes: 0

Burcu Dogan
Burcu Dogan

Reputation: 9213

The only reliable way to embed a file is to embed its embedLink for Docs files. Other files are forcefully downloaded, so you need to have a proxy to serve them as Web pages.

Upvotes: 2

Related Questions