【问题标题】:Google Spreadsheet Java API returning no spreadsheetsGoogle 电子表格 Java API 不返回电子表格
【发布时间】:2016-04-25 13:31:07
【问题描述】:

我需要有关 Google 电子表格 Java API 的帮助。我得到一个空的电子表格列表,即使我有电子表格。

List<SpreadsheetEntry> spreadsheets = feed.getEntries();

在我的代码中不返回任何条目。而当我在浏览器中运行 URL http://spreadsheets.google.com/feeds/spreadsheets/private/full 时,我可以看到 JSON 中的电子表格条目。

这是为什么?我怎样才能解决这个问题?这是我的代码:

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;

public class TestSpreadsheet {

    private static final String SERVICE_ACCOUNT_EMAIL = "xxx@yyyy-zzzz.iam.gserviceaccount.com";

    private static final File SERVICE_ACCOUNT_PKCS12_FILE = new File("./key.p12");
    private static SpreadsheetService SPREADSHEETSERVICE;

    public static void main(String[] args) throws GeneralSecurityException, IOException, ServiceException {


           HttpTransport httpTransport = new NetHttpTransport();
           JacksonFactory jsonFactory = new JacksonFactory();
           String [] SCOPESArray= {"https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds"};

           List<String> SCOPES = Arrays.asList("https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds");

           //final List<String> SCOPES = Arrays.asList(SCOPESArray);
           GoogleCredential credential = new GoogleCredential.Builder()
             .setTransport(httpTransport)
             .setJsonFactory(jsonFactory)
             .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
             .setServiceAccountScopes(SCOPES)
             .setServiceAccountPrivateKeyFromP12File(SERVICE_ACCOUNT_PKCS12_FILE)
             .build();
           credential.refreshToken();
           SPREADSHEETSERVICE = new SpreadsheetService("test");
           SPREADSHEETSERVICE.setOAuth2Credentials(credential);

           URL SPREADSHEET_FEED_URL;
            SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

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

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

}

【问题讨论】:

  • 请详细说明您的问题。您是否尝试过任何调试?您尝试了什么,得到了哪些结果?
  • 是的,我试过调试它。问题是代码 List 电子表格 = feed.getEntries();不返回任何条目。而当我在浏览器中运行 url spreadsheets.google.com/feeds/spreadsheets/private/full 时,我可以在 JSON 中看到电子表格条目。
  • 能否提供您的错误日志?
  • 我没有收到任何错误,但没有返回电子表格,如果 (spreadsheets.size() == 0) { System.out.println("未找到电子表格。"); } 计算结果为真。

标签: java oauth-2.0 spreadsheet google-spreadsheet-api


【解决方案1】:

我找到了解决这个问题的方法。我们需要共享带有服务帐号 ID 的电子表格,例如上例中 xxx@yyyy-zzzz.iam.gserviceaccount.com 是服务帐号 ID,我们需要使用此 ID 共享电子表格。

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多