gpt4 book ai didi

java - Google Spreadsheet Java API 不返回电子表格

转载 作者:行者123 更新时间:2023-11-30 07:25:40 26 4
gpt4 key购买 nike

我需要有关 Google Spreadsheets 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.");
}
}

}

最佳答案

我找到了这个问题的解决方案。我们需要与服务帐户 ID 共享电子表格,例如在上面的情况下 xxx@yyyy-zzzz.iam.gserviceaccount.com 是服务帐户 ID,我们需要与此 ID 共享电子表格。

关于java - Google Spreadsheet Java API 不返回电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com