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

问题描述:

我在console.developers.google.com创建了一个项目,并且我创建了P12键。 我创建了一个新的客户端ID,我在下面的代码中使用了电子邮件地址。谷歌电子表格API - com.google.gdata.util.ParseException:无效的根元素

据我所知,身份验证总是成功的。我认为我在Feed网址中遇到问题,但我不确定。

当我试图从我的谷歌电子表格中读取数据时,出现提到的错误。

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("[email protected]") 
      .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("[email protected]") 
      .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(); 
     } 

    } 

} 

} 

希望这有助于找出谁已挫败谷歌的差劲的文档的人。 ;)

+1

干得好!感谢您的贡献! – rpax

+0

@ rpax的快感;) –