Google Spreadsheet Java API returning no spreadshe

2019-07-22 20:19发布

问题:

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

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

}

回答1:

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.