无需身份验证即可解析Google电子表格网址

时间:2013-09-09 12:45:06

标签: java google-sheets gdata-api

我的目标是从Google电子表格网址无需身份验证中检索CellFeeds。 我尝试使用以下电子表格网址(发布到网络): https://docs.google.com/spreadsheet/ccc?key=0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE&usp=sharing

此URL存储在变量“spreadsheetName”中。 首先尝试将整个URL 作为Service.getFeed()的参数 url = new URL(spreadsheetName); WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);

但后来我遇到了以下异常:

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

第二次尝试是使用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);

......我得到了下一个例外:

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

您是否有任何想法我做错了什么,或者是否有人在未经身份验证的情况下成功检索了电子表格网址中的数据? Thx提前!

2 个答案:

答案 0 :(得分:1)

你有两个问题。我不确定第二个问题,但首先是你试图使用没有正确密钥的cellFeedURL,你只是使用worksheetName,这可能不正确。如果您这样做:

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);
}

您将获得正确的CellFeed。但是,您的第二个问题是,如果您这样做,CellEntry.getCell()中的所有CellFeed都会填充为null。我不确定原因,或者以public/basic登录时是否可以解决。

答案 1 :(得分:1)

以下代码适用于您的第一个问题。可能第二个问题是由CellFeed中的查询参数引起的。确保其他相关的罐子可用。我曾经在电子表格API上工作过。这可能对你有帮助。

    import java.net.URL;
    import java.lang.*;
    import java.util.List;
    import com.google.gdata.client.spreadsheet.FeedURLFactory;
    import com.google.gdata.client.spreadsheet.ListQuery;
    import com.google.gdata.client.spreadsheet.SpreadsheetService;
    import com.google.gdata.data.spreadsheet.CustomElementCollection;
    import com.google.gdata.data.spreadsheet.ListEntry;
    import com.google.gdata.data.spreadsheet.ListFeed;
    import com.google.gdata.data.spreadsheet.WorksheetEntry;
    import com.google.gdata.data.spreadsheet.WorksheetFeed;

    public class SpreadsheetsDemo {
        public static void main(String[] args) throws Exception{
            String application = "SpreadsheetsDemo";
            String key = "0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE";

            SpreadsheetService service = new SpreadsheetService(application);

            URL url = FeedURLFactory.getDefault().getWorksheetFeedUrl(key, "public", "basic");

            WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);
            List<WorksheetEntry> worksheetList = feed.getEntries();
            WorksheetEntry worksheetEntry = worksheetList.get(0);
            CellQuery cellQuery = new CellQuery(worksheetEntry.CellFeedLink);
            CellFeed cellFeed = service.Query(cellQuery);

            foreach (CellEntry cell in cellFeed.Entries)
            {
                //Iterate through the columns and rows
            }
         }
        }