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

Java google电子表格API-com.google.gdata.util.ParseException:无效的根元素,java,google-docs,google-spreadsheet-api,Java,Google Docs,Google Spreadsheet Api,我在console.developers.google.com上创建了一个项目,并创建了P12密钥。 我创建了一个新的客户ID,并在下面的代码中使用了其中的电子邮件地址 据我所知,身份验证总是成功的。我想我在feedurl中遇到了问题,但我不确定 当我试图从我的谷歌电子表格中读取数据时,我得到了上面提到的错误 com.google.gdata.util.ParseException: [Line 1, Column 165] Invalid root element, expected (nam

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

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

当我试图从我的谷歌电子表格中读取数据时,我得到了上面提到的错误

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

}

}
公共类TestingGroundApp2{
publicstaticvoidmain(字符串[]args)抛出畸形的DurLexception、GeneralSecurityException、IOException和ServiceException{
URL电子表格\u提要\u URL;
电子表格\u提要\u URL=新URL(“https://spreadsheets.google.com/feeds/spreadsheets/16mMy_UmT1MSJzDMWiQ8gjqRv4d9JPoC8Xwf76URkVqQ");
文件p12=新文件(“C:\\Users\\USERNAME\\Desktop\\key.p12”);
HttpTransport HttpTransport=新的NetHttpTransport();
JacksonFactory jsonFactory=新JacksonFactory();
字符串[]范围数组={”https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
最终列表SCOPES=Arrays.asList(SCOPESArray);
GoogleCredential credential=新建GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(“MYEMAIL_地址_CREDENTIALS@developer.gserviceaccount.com")
.setServiceAccountScopes(范围)
.SetServiceAccountPrivateKeyfromP12文件(p12)
.build();
电子表格服务=新的电子表格服务(“电子表格阅读器”);
服务。设置OAuth2Credentials(凭证);
SpreadsheetFeed=service.getFeed(电子表格\u feed\u URL,SpreadsheetFeed.class);
列表电子表格=feed.getEntries();
如果(电子表格.size()==0){
System.out.println(“未找到电子表格”);
}
SpreadsheetEntry电子表格=空;
对于(int i=0;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();
        }

    }

}

}
公共类TestingGroundApp2{
publicstaticvoidmain(字符串[]args)抛出畸形的DurLexception、GeneralSecurityException、IOException和ServiceException{
URL电子表格\u提要\u URL;
电子表格\u提要\u URL=新URL(“https://spreadsheets.google.com/feeds/spreadsheets/private/full/16mMy_UmT1MSJzDMWiQ8gjqRv4d9JPoC8Xwf76URkVqQ");
文件p12=新文件(“C:\\Users\\USERNAME\\Desktop\\key.p12”);
HttpTransport HttpTransport=新的NetHttpTransport();
JacksonFactory jsonFactory=新JacksonFactory();
字符串[]范围数组={”https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
最终列表SCOPES=Arrays.asList(SCOPESArray);
GoogleCredential credential=新建GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(“MYEMAIL_地址_CREDENTIALS@developer.gserviceaccount.com")
.setServiceAccountScopes(范围)
.SetServiceAccountPrivateKeyfromP12文件(p12)
.build();
电子表格服务=新的电子表格服务(“电子表格阅读器”);
服务。设置OAuth2Credentials(凭证);
//向API发出请求并获取所有电子表格。
SpreadsheetEntry电子表格=service.getEntry(电子表格\u提要\u URL,
电子表格输入类);
System.out.println(电子表格.getTitle().getPlainText());
//向API发出请求以获取有关所有API的信息
//电子表格中的工作表。
列表工作表=电子表格。getWorksheets();
//迭代电子表格中的每个工作表。
用于(工作表工作表:工作表){
//获取工作表的标题、行数和列数。
字符串标题=工作表.getTitle().getPlainText();
int rowCount=worksheet.getRowCount();
int colCount=工作表。getColCount();
//将提取的信息打印到此工作表的屏幕上。
System.out.println(“\t”+title+”-行:“+rowCount+”列:“+colCount”);
//获取工作表的列表提要。
URL listFeedUrl=工作表。getListFeedUrl();
ListFeed ListFeed=service.getFeed(listFeedUrl,ListFeed.class);
//遍历每一行,打印其单元格值。
对于(ListEntry行:listFeed.getEntries()){
//打印第一列的单元格值
System.out.print(row.getTitle().getPlainText()+“\t”);
//迭代其余列,并打印每个单元格值
for(字符串标记:row.getCustomElements().getTags()){
System.out.print(row.getCustomElements().getValue(标记)+“\t”);
}
System.out.println();
}
}
}
}
希望这能帮助那些对谷歌糟糕的文档感到沮丧的人