简体   繁体   English

如何在未经身份验证的情况下使用Google Spreadsheet API Java库获取公开的Google电子表格数据

[英]How to get public Google Spreadsheet data using Google Spreadsheet API Java library without authentication

-what I want to do -我想做的事

I would like to get data from Google Spreadsheet using Google Spreadsheet API Java library without authentication. 我想使用Google Spreadsheet API Java库从Google Spreadsheet获取数据而无需身份验证。 The Google Spreadsheet is published with public. Google电子表格随公开发布。 I would like to use the following method: com.google.gdata.data.spreadsheet.CustomElementCollection 我想使用以下方法:com.google.gdata.data.spreadsheet.CustomElementCollection

-Issue -问题

CustomElementCollection return collect data with authentication. CustomElementCollection返回使用身份验证收集数据。 But CustomElementCollection return null without authentication. 但是CustomElementCollection在没有身份验证的情况下返回null。

As listEntry.getPlainTextContent() shows data, so I think I should be able to get the data in any ways. 由于listEntry.getPlainTextContent()显示数据,所以我认为我应该能够以任何方式获取数据。

-Source code attached - 附加源代码

With authentication: Auth.java 使用身份验证: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") );
        }
    }
}

Without authentication: NoAuth.java 没有身份验证: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") );
        }
    }
}

Google Spreadsheet: Google电子表格:

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

-Result -结果

Without authentication 没有身份验证

content=[age: 23] name=null age=null content = [age:23] name = null age = null

With authentication 有了身份验证

content=[age: 23] name=Taro age=23 content = [年龄:23​​]姓名=太郎年龄= 23岁

Please let me know the useful information to avoid the issue. 请让我知道有用的信息,以避免这个问题。

I don't know why it works like that, but when you don't access request with credentials, you are not able to retrieve cells via: 我不知道它为什么会这样,但是当你不使用凭证访问请求时,你无法通过以下方式检索单元格:

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

I've tested it and I have found only this way to retrieve data: 我测试了它,我发现只有这种方式来检索数据:

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

It prints: 它打印:

Taro    age: 23
Hanako  age: 16

As you see, you should parse text and retrieve age from raw String . 如您所见,您应该解析文本并从原始String检索年龄。

I believe the problem is that you are using the "basic" projection for your spreadsheet. 我认为问题在于您正在使用电子表格的"basic"投影。 If you use the "values" projection, everything should work as expected. 如果您使用"values"投影,一切都应该按预期工作。

I was wondering about this as well. 我也想知道这件事。 I looked at the feed coming in (just paste the URL to the sheet into Chrome), and it seems like there is no XML markup, and are all coming in under the <content> tag. 我查看了进入的Feed(只是将表格中的网址粘贴到Chrome中),似乎没有XML标记,并且全部都在<content>标记下。 So it makes sense that the parser is lumping it all into the text content of the BaseEntry (instead of making a ListEntry). 因此,解析器将其全部集中到BaseEntry的文本内容(而不是生成ListEntry)是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取通过Java与Google API公开的电子表格? - How to read spreadsheet which made public using Java with google api? 使用未经身份验证的Google电子表格 - Using Google spreadsheet without authentication 如何使用Java和Google Spreadsheet API获取Google电子表格中单元格的内容 - How to get the contents of a cell in a google spreadsheet using java and Google Spreadsheet API 使用Google Spreadsheet Java API将数据插入Google电子表格 - Inserting data into Google spreadsheet with Google Spreadsheet Java API 使用Google Spreadsheet API在Google中的Google驱动器中创建电子表格 - Create Spreadsheet using Google Spreadsheet API in Google drive in Java 在Java中使用Google电子表格api创建Google电子表格 - creating a google spreadsheet using google spreadsheet api in java 如何使用Java将Google文档(电子表格)设置为公开? - How to set a Google Doc (Spreadsheet) to public using Java? 使用带有Java的API在Google电子表格中插入一行 - insert a row in to a google spreadsheet using the API with java 无法使用Google电子表格API访问电子表格 - Not able to access spreadsheet using google spreadsheet API 如何使用Java Google Doc API共享电子表格 - How to share a spreadsheet using Java Google Doc API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM