Java 无需身份验证即可解析Google电子表格URL

Java 无需身份验证即可解析Google电子表格URL,java,google-sheets,gdata-api,Java,Google Sheets,Gdata Api,我的目标是从Google电子表格URL中检索CellFeed,而无需验证。 我尝试了以下电子表格URL(发布到web): 此URL存储在变量“spreadsheetName”中。 第一次尝试是将整个URL作为Service.getFeed()的参数。 url=新url(电子表格名称); WorksheetFeed提要=service.getFeed(url,WorksheetFeed.class); 但后来我遇到了以下例外情况: com.google.gdata.util.RedirectR

我的目标是从Google电子表格URL中检索CellFeed,而无需验证。 我尝试了以下电子表格URL(发布到web):

此URL存储在变量“spreadsheetName”中。 第一次尝试是将整个URL作为Service.getFeed()的参数。
url=新url(电子表格名称);
WorksheetFeed提要=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
…我得到了下一个例外:

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

你知道我做错了什么吗?或者有没有人在没有身份验证的情况下成功地从电子表格URL检索数据?提前谢谢

您有两个问题。我不确定第二个问题,但第一个问题是,如果没有正确的键,您试图使用
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
。但是,您的第二个问题是,如果这样做,则
CellFeed
中的所有
CellEntry.getCell()
都将填充为
null
。我不知道为什么,或者在以
public/basic
身份登录时是否可以解决此问题。以下代码适用于您的第一个问题。第二个问题可能是由于CellFeed中的查询参数。确保其他相关JAR是否可用。很久以前我就在做电子表格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
            }
         }
        }
import java.net.URL;
导入java.lang.*;
导入java.util.List;
导入com.google.gdata.client.spreadsheet.FeedURLFactory;
导入com.google.gdata.client.spreadsheet.ListQuery;
导入com.google.gdata.client.spreadsheet.spreadsheet服务;
导入com.google.gdata.data.spreadsheet.CustomElementCollection;
导入com.google.gdata.data.spreadsheet.ListEntry;
导入com.google.gdata.data.spreadsheet.ListFeed;
导入com.google.gdata.data.spreadsheet.WorksheetEntry;
导入com.google.gdata.data.spreadsheet.WorksheetFeed;
公共类电子表格DEMO{
公共静态void main(字符串[]args)引发异常{
字符串application=“SpreadsheetsDemo”;
字符串键=“0AVNWODP9TASIDERSBFRNNXDSN2X4MXUMLYY0G3VUE”;
电子表格服务=新电子表格服务(应用);
URL URL=FeedURLFactory.getDefault().getWorksheetFeedUrl(键“public”、“basic”);
WorksheetFeed提要=service.getFeed(url,WorksheetFeed.class);
List worksheetList=feed.getEntries();
WorksheetEntry WorksheetEntry=工作表列表.get(0);
CellQuery CellQuery=新的CellQuery(worksheetEntry.CellFeedLink);
CellFeed CellFeed=service.Query(cellQuery);
foreach(cellFeed.Entries中的CellEntry单元格)
{
//遍历列和行
}
}
}

现在我可以获取CellEntry对象,但在尝试获取单元格本身时,只有空值。就像前面提到的@durron697一样。无论如何,谢谢你的帮助。
    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
            }
         }
        }