google 电子表格 API - com.google.gdata.util.ParseException: 无效的根元素

google spreadsheet API - com.google.gdata.util.ParseException: Invalid root element

我在 console.developers.google.com 创建了一个项目,并创建了 P12 密钥。 我创建了一个新的客户端 ID,并在下面的代码中使用了那里的电子邮件地址。

据我所知,身份验证总是成功的。我想我在 Feed URL 中遇到了问题,但我不确定。

当我尝试从我的 google 电子表格中读取数据时,出现上述错误。

com.google.gdata.util.ParseException: [Line 1, Column 165] Invalid root element, expected (namespace uri:local name) of (http://www.w3.org/2005/Atom:feed), found (http://www.w3.org/2005/Atom:entry

这是我的代码:

public class TestingGroundApp2 {

public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {
    URL SPREADSHEET_FEED_URL;
    SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/16mMy_UmT1MSJzDMWiQ8gjqRv4d9JPoC8Xwf76URkVqQ");

    File p12 = new File("C:\Users\USERNAME\Desktop\key.p12");

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
    final List SCOPES = Arrays.asList(SCOPESArray);
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId("MYEMAIL_ADDRESS_CREDENTIALS@developer.gserviceaccount.com")
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKeyFromP12File(p12)
            .build();

    SpreadsheetService service = new SpreadsheetService("SpreadsheetReader");

    service.setOAuth2Credentials(credential);
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    if (spreadsheets.size() == 0) {
        System.out.println("No spreadsheets found.");
    }

    SpreadsheetEntry spreadsheet = null;
    for (int i = 0; i < spreadsheets.size(); i++) {
        if (spreadsheets.get(i).getTitle().getPlainText().startsWith("Sheet1")) {
            spreadsheet = spreadsheets.get(i);
            System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());
            System.out.println("ID of SpreadSheet: " + i);
        }
    }

}

}

有人能指出问题吗?

经过一些洗牌,我终于成功了。 事实证明,我所要做的就是将 SpreadsheetFeed 更改为 SpreadsheetEntry。

下面是编辑后的代码。

public class TestingGroundApp2 {

public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {
    URL SPREADSHEET_FEED_URL;
    SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full/16mMy_UmT1MSJzDMWiQ8gjqRv4d9JPoC8Xwf76URkVqQ");

    File p12 = new File("C:\Users\USERNAME\Desktop\key.p12");

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
    final List SCOPES = Arrays.asList(SCOPESArray);
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId("MYEMAIL_ADDRESS_CREDENTIALS@developer.gserviceaccount.com")
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKeyFromP12File(p12)
            .build();

    SpreadsheetService service = new SpreadsheetService("SpreadsheetReader");

    service.setOAuth2Credentials(credential);

    // Make a request to the API and get all spreadsheets.
    SpreadsheetEntry spreadsheet = service.getEntry(SPREADSHEET_FEED_URL,
            SpreadsheetEntry.class);

    System.out.println(spreadsheet.getTitle().getPlainText());

    // Make a request to the API to fetch information about all
    // worksheets in the spreadsheet.
    List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();

    // Iterate through each worksheet in the spreadsheet.
    for (WorksheetEntry worksheet : worksheets) {
        // Get the worksheet's title, row count, and column count.
        String title = worksheet.getTitle().getPlainText();
        int rowCount = worksheet.getRowCount();
        int colCount = worksheet.getColCount();

        // Print the fetched information to the screen for this worksheet.
        System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);
        // Fetch the list feed of the worksheet.
        URL listFeedUrl = worksheet.getListFeedUrl();
        ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);

        // Iterate through each row, printing its cell values.
        for (ListEntry row : listFeed.getEntries()) {
            // Print the first column's cell value
            System.out.print(row.getTitle().getPlainText() + "\t");
            // Iterate over the remaining columns, and print each cell value
            for (String tag : row.getCustomElements().getTags()) {
                System.out.print(row.getCustomElements().getValue(tag) + "\t");
            }
            System.out.println();
        }

    }

}

}

希望这能帮助那些因 google 的糟糕文档而感到沮丧的人。 ;)