【问题标题】:google spreadsheet API - com.google.gdata.util.ParseException: Invalid root element谷歌电子表格 API - com.google.gdata.util.ParseException:无效的根元素
【发布时间】:2015-07-19 16:32:34
【问题描述】:

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

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

当我尝试从我的谷歌电子表格中读取数据时,我收到了上述错误。

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

}

}

谁能指出问题所在?

【问题讨论】:

    标签: java google-docs google-spreadsheet-api


    【解决方案1】:

    在做了一些洗牌之后,我终于设法让它工作了。 事实证明,我所要做的就是将 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();
            }
    
        }
    
    }
    
    }
    

    希望这可以帮助那些因谷歌糟糕的文档而感到沮丧的人。 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多