如何从java中的谷歌电子表格中检索数据?

时间:2017-01-18 00:12:18

标签: java api google-sheets google-speech-api

我正在尝试使用java程序从谷歌电子表格中检索数据,但在某些步骤中感到困惑。 SpreadSheet:https://docs.google.com/spreadsheets/d/1P3ViZ0NWFj2TY8P9h6C1LNCo8BUuCqOuhypEKxGoRY0/edit#gid=0

困惑1: 我应该在网址中加入我的密码/ ID吗? : SPREADSHEET_FEED_URL =新网址(" https://spreadsheets.google.com/feeds/spreadsheets/private/full");

或  SPREADSHEET_FEED_URL =新网址(" https://spreadsheets.google.com/feeds/1P3ViZ0NWFj2TY8P9h6C1LNCo8BUuCqOuhypEKxGoRY0/private/full");

困惑2: 我应该使用setServiceAccountID还是setServiceAccountUser?

到目前为止我的代码:

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;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class OAuthIntegration{
    public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {
        URL SPREADSHEET_FEED_URL;

        //Is this correct ?
        SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/1P3ViZ0NWFj2TY8P9h6C1LNCo8BUuCqOuhypEKxGoRY0/private/full");

        File p12 = new File("My Project-ae4990736d1f.p12");

        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
        final List SCOPES = Arrays.asList(SCOPESArray);

        //Is this correct ? 
        GoogleCredential credential = new GoogleCredential.Builder()
                   .setTransport(httpTransport)
                   .setJsonFactory(jsonFactory)
                   .setServiceAccountUser("xxxx@gmail.com")
                   .setServiceAccountScopes(SCOPES)
                   .setServiceAccountPrivateKeyFromP12File(p12)
                   .build();

        SpreadsheetService service = new SpreadsheetService("Test");

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

         SpreadsheetEntry spreadsheet = null;
        for (int i = 0; i < spreadsheets.size(); i++) {
            if (spreadsheets.get(i).getTitle().getPlainText().startsWith("ListOfSandboxes")) {
                spreadsheet = spreadsheets.get(i);
                System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());
                System.out.println("ID of SpreadSheet: " + i);
            }
        }

    }

}

我收到以下错误:

Exception in thread "main" com.google.gdata.util.AuthenticationException: Failed to refresh access token: 401 Unauthorized
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:260)
    at com.google.gdata.client.GoogleAuthTokenFactory.handleSessionExpiredException(GoogleAuthTokenFactory.java:702)
    at com.google.gdata.client.GoogleService.handleSessionExpiredException(GoogleService.java:738)
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:649)
    at com.google.gdata.client.Service.getFeed(Service.java:1017)
    at fypMain.OAuthIntegration.main(OAuthIntegration.java:42)
Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:258)
    ... 5 more

提前谢谢!

0 个答案:

没有答案