我没有从Google文档中获取电子表格

时间:2015-07-30 11:54:26

标签: java google-api google-sheets

我从here举了示例。但是我的代码下面给出了“没有找到电子表格”,但我在这个网址https://spreadsheets.google.com/feeds/spreadsheets/private/full上有几个电子表格。 我不明白为什么我没有得到任何电子表格。

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

    SpreadsheetService service = new SpreadsheetService("GoogleSheet");

    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.");
    }     else {
        SpreadsheetEntry spreadsheetEntry = spreadsheets.get(0);
        System.out.println(spreadsheetEntry.getTitle().getPlainText());
    }

1 个答案:

答案 0 :(得分:-1)

您应该共享从Google驱动器创建的电子表格,如下所示:

client id="132465@developer.gserviceaccount.com";

希望这有帮助