How to integrate Google Document Viewer API

I’m working on a project where I need to display documents using Google’s viewer service. I want to upload files and get embeddable URLs that I can use on my website.

Right now I can only view public documents by using the standard viewer URL format like docs.google.com/viewer?url=document_link. But I need something more flexible for private documents.

I tried using the embed functionality with Java but I keep getting errors. Here’s what I’m attempting:

public class DocumentEmbedder {
    private DocsService docService;
    
    public void setupEmbedLink(String fileName) {
        try {
            DocumentListEntry fileEntry = new DocumentListEntry();
            fileEntry.setTitle(new PlainTextConstruct(fileName));
            
            // This is where I get the error
            URL embedUrl = new URL("https://docs.google.com/feeds/default/private/full");
            docService.insert(embedUrl, fileEntry);
            
        } catch (Exception error) {
            System.out.println("Error: " + error.getMessage());
        }
    }
}

The error I’m getting is:
com.google.gdata.util.InvalidEntryException: Invalid request URI

Is there a way to programmatically create embed links for documents? I need users to only have view permissions, no editing allowed. Any working Java examples would be really helpful.

yeah, that old documents api is dead - i’ve hit the same wall. here’s what worked: upload to drive normally, grab the file id from the response, then embed with <iframe src="https://drive.google.com/file/d/FILE_ID/preview"></iframe>. works perfectly for view-only. just make sure you set sharing to ‘anyone with link’ or other people won’t see it.

The Google Documents List API has been deprecated, leading to issues like the InvalidEntryException you’re encountering. To solve this, you should transition to the Google Drive API v3. Start by uploading your document to Google Drive, ensuring you set the appropriate permissions. From the file metadata, retrieve the webViewLink. Keep in mind that private documents require viewer authentication. I recommend using a service account, placing files in a designated folder, and granting permissions for ‘anyone with the link can view’. This approach keeps your links semi-private and eliminates the need for user logins. The Drive API documentation provides comprehensive Java examples for handling uploads and permissions, so refer to files.create() for uploads and permissions.create() for setting access control, which offers a much more stable solution than your current method.

You’re getting that error because Google killed the Documents List API years ago. I switched to Drive API v3 with iframe embedding and it works great. Once you upload through Drive API, just use this URL pattern: https://drive.google.com/file/d/{FILE_ID}/preview. Make sure you set sharing permissions during upload - I use type: 'anyone', role: 'reader'. For Java, the Drive API’s files().create() method is way more reliable than the old stuff. Heads up though - sometimes newly uploaded files need a few seconds before the embed URL works, so you might need to add a small delay or retry. The preview URLs are perfect for view-only access and handle auth automatically if users are signed into Google accounts with permission.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.