Search code examples
javagoogle-sheetsgdata-api

Parse Google spreadsheet URL without authentication


My aim is to retrieve CellFeeds from Google spreadsheet URLs without authentication. I tried it with the following spreadsheet URL (published to web): https://docs.google.com/spreadsheet/ccc?key=0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE&usp=sharing

This URL is stored in variable "spreadsheetName". First attempt was to take the whole URL as argument for Service.getFeed().
url = new URL(spreadsheetName); WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);

But then I ran into following exception :

com.google.gdata.util.RedirectRequiredException: Found
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved

Second attempt was to build the URL with the key from the origin URL, using FeedURLFactory:

String key = spreadsheetName.split("key=")[1].substring(0, 44);
url = FeedURLFactory.getDefault().getCellFeedUrl(key,
                    worksheetName, "public", "basic");
WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);

...and I got the next exception:

com.google.gdata.util.InvalidEntryException: Bad Request
Invalid query parameter value for grid-id.

Do you have any ideas what I did wrong or is there anybody who successfully retrieved data from spreadsheet URLs without authentication? Thx in advance!


Solution

  • You have two problems. I'm not sure about the second problem, but the first is that you are trying to use a cellFeedURL without the correct key, you are just using worksheetName, which is probably not correct. If you do something like this:

    public static void main(String... args) throws MalformedURLException, ServiceException, IOException {
        SpreadsheetService service = new SpreadsheetService("Test");
        FeedURLFactory fact = FeedURLFactory.getDefault();
    
        String key = "0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE";
        URL spreadSheetUrl = fact.getWorksheetFeedUrl(key, "public", "basic");
        WorksheetFeed feed = service.getFeed(spreadSheetUrl,
                WorksheetFeed.class);
    
        WorksheetEntry entry = feed.getEntries().get(0);
        URL cellFeedURL = entry.getCellFeedUrl();
        CellFeed cellFeed = service.getFeed(cellFeedURL, CellFeed.class);
    }
    

    You will get the correct CellFeed. However, your second problem is that if you do it this way, all the CellEntry.getCell() in the CellFeed populate as null. I am not sure why, or if it can be solved while logged in as public/basic.