9

得到公众的谷歌电子表格数据 - 什么我想要做的如何使用谷歌电子表格API的Java库没有认证

我想获得使用谷歌电子表格API的Java库从谷歌电子表格数据,而无需认证。 Google电子表格与公开发布。 我想用下面的方法: com.google.gdata.data.spreadsheet.CustomElementCollection

-issue

CustomElementCollection回报收集数据与认证。 但是CustomElementCollection在没有验证的情况下返回null。

由于listEntry.getPlainTextContent()显示数据,所以我认为我应该能够以任何方式获取数据。

- 源代码附

随着认证:Auth.java

import java.net.URL; 
import java.util.List; 
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.SpreadsheetEntry; 
import com.google.gdata.data.spreadsheet.WorksheetEntry; 

public class Auth { 
    public static void main(String[] args) throws Exception{ 
     String applicationName = "AppName"; 
     String user = args[0]; 
     String pass = args[1]; 
     String key = args[2]; 
     String query = args[3]; 

     SpreadsheetService service = new SpreadsheetService(applicationName); 
     service.setUserCredentials(user, pass); //set client auth 

     URL entryUrl = new URL("http://spreadsheets.google.com/feeds/spreadsheets/" + key); 
     SpreadsheetEntry spreadsheetEntry = service.getEntry(entryUrl, SpreadsheetEntry.class); 
     WorksheetEntry worksheetEntry = spreadsheetEntry.getDefaultWorksheet(); 

     ListQuery listQuery = new ListQuery(worksheetEntry.getListFeedUrl()); 
     listQuery.setSpreadsheetQuery(query); 

     ListFeed listFeed = service.query(listQuery, ListFeed.class); 
     List<ListEntry> list = listFeed.getEntries(); 
     for(ListEntry listEntry : list) 
     { 
      System.out.println("content=[" + listEntry.getPlainTextContent() + "]"); 
      CustomElementCollection elements = listEntry.getCustomElements(); 
      System.out.println(
        " name=" + elements.getValue("name") + 
        " age=" + elements.getValue("age")); 
     } 
    } 
} 

无认证:NoAuth.java

import java.net.URL; 
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 NoAuth { 
    public static void main(String[] args) throws Exception{ 
     String applicationName = "AppName"; 
     String key = args[0]; 
     String query = args[1]; 

     SpreadsheetService service = new SpreadsheetService(applicationName); 

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

     ListQuery listQuery = new ListQuery(worksheetEntry.getListFeedUrl()); 
     listQuery.setSpreadsheetQuery(query); 

     ListFeed listFeed = service.query(listQuery, ListFeed.class); 
     List<ListEntry> list = listFeed.getEntries(); 
     for(ListEntry listEntry : list) 
     { 
      System.out.println("content=[" + listEntry.getPlainTextContent() + "]"); 
      CustomElementCollection elements = listEntry.getCustomElements(); 
      System.out.println(
        " name=" + elements.getValue("name") + 
        " age=" + elements.getValue("age")); 
     } 
    } 
} 

谷歌电子表格:

https://docs.google.com/spreadsheet/pub?key=0Ajawooo6A9OldHV0VHYzVVhTZlB6SHRjbGc5MG1CakE&output=html

-Result

没有认证

含量= [年龄:23​​] 名= NULL年龄= NULL

随着认证

含量= [年龄:23​​] 名=太郎年龄= 23

请让我知道有用的信息,以避免这个问题。

回答

3

我不知道为什么它的工作原理类似,但是当你不访问与证书的要求,你是不是能够通过检索细胞:

CustomElementCollection elements = listEntry.getCustomElements(); 
System.out.println(" name=" + elements.getValue("name") + " age=" + elements.getValue("age")); 

我测试过它,我有只发现了这种方式来检索数据:

List<ListEntry> list = listFeed.getEntries(); 
for (ListEntry row : list) { 
    System.out.println(row.getTitle().getPlainText() + "\t" 
      + row.getPlainTextContent()); 
} 

它打印:

Taro age: 23 
Hanako age: 16 

正如你看到的,你应该分析文本和r从原始String提取时间。

+0

谢谢你的时间和精力!我会尝试。 – user2128521 2013-03-04 07:52:58

+0

请看看euclio的回答。它解释了为什么问题中的代码不起作用。 – xhe8 2015-02-18 06:31:11

0

我在想这件事。我查看了即将发布的Feed(只需将网址粘贴到Chrome中),并且好像没有XML标记,并且都在<内容>标记下。因此,解析器将所有内容整合到BaseEntry的文本内容中(而不是制作ListEntry)是有道理的。

2

我相信问题是,你正在使用的"basic"投影为您的电子表格。如果您使用"values"投影,一切都应按预期工作。

+0

这不提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 – 2014-12-22 07:57:19

+3

你是什么意思?被接受的答案当然不能回答这个问题。从基本投影切换到值投影解决了问题。 – euclio 2014-12-28 02:04:17

+0

我认为这应该是正确的答案。我已经测试过它。被接受的答案是有点工作,但并没有完成原始问题的真正需要。 – xhe8 2015-02-18 06:26:46

相关问题