Google Spreadsheet Java API不返回电子表格 [英] Google Spreadsheet Java API returning no spreadsheets

查看:148
本文介绍了Google Spreadsheet Java API不返回电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need help with the Google Spreadsheets Java API. I am getting an empty list of spreadsheets, even when I have spreadsheets.

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

我的代码中的

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

in my code doesn't return any entries. Whereas when I run the URL http://spreadsheets.google.com/feeds/spreadsheets/private/full in the browser, I can see the spreadsheet entries in the JSON.

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

Why is this? How can I fix this? Here is my code:

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

}

推荐答案

我找到了解决此问题的方法.例如,在xxx@yyyy-zzzz.iam.gserviceaccount.com是服务帐户ID的情况下,我们需要使用服务帐户ID共享电子表格.我们需要使用该ID共享电子表格.

I found the solution to this problem. We need to share the spreadsheet with service account id for example in the case above xxx@yyyy-zzzz.iam.gserviceaccount.com is service account id, and we need to share the spreadsheets with this id.

这篇关于Google Spreadsheet Java API不返回电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆