【问题标题】:Parse Google spreadsheet URL without authentication无需身份验证即可解析 Google 电子表格 URL
【发布时间】:2013-09-12 23:01:56
【问题描述】:

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

此 URL 存储在变量“电子表格名称”中。 第一次尝试是将整个 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 使用 key 从原始 URL 构建 URL:

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.

您是否知道我做错了什么,或者是否有人在未经身份验证的情况下成功地从电子表格 URL 检索数据?提前谢谢!

【问题讨论】:

    标签: java google-sheets gdata-api


    【解决方案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。但是,您的第二个问题是,如果您这样做,CellFeed 中的所有CellEntry.getCell() 都会填充为null。我不知道为什么,或者以public/basic 登录时是否可以解决。

    【讨论】:

      【解决方案2】:

      以下代码应该适用于您的第一个问题。由于 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
                  }
               }
              }
      

      【讨论】:

      • 现在我可以获取 CellEntry 对象,但是在尝试获取 Cell 本身时只有空值。就像已经提到的@durron697 一样。无论如何感谢您的帮助。
      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多