-我想做的事
我想使用 Google Spreadsheet API Java 库从 Google Spreadsheet 获取数据,无需身份验证。Google 电子表格公开发布。我想使用以下方法:com.google.gdata.data.spreadsheet.CustomElementCollection
-问题
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
-结果
无需认证
内容=[年龄:23] 名称=null 年龄=null
带认证
内容=[年龄:23] 名称=芋头年龄=23
请让我知道有用的信息以避免该问题。